The first part is easy but it won't solve your problems.
HANDLE hRecvThread = NULL;
The second part is more difficult. This is not the way to terminate a thread.
1. You must allow the thread main to exit itself. This is a tricky process if the thread is blocked (waiting).
2. You can then wait on the thread handle to be signaled indicating thread has exited.
3. You may then call CloseHandle() on the thread handle.
This gives you the idea.
dwResult = ::WaitForSingleObject(m_hThread, killTimeOut);
switch(dwResult)
{
case WAIT_OBJECT_0:
::CloseHandle(m_hThread);
m_hThread = NULL;
break;
case WAIT_TIMEOUT:
default:
bRetVal = false;
break;
}
The overheads and time delays involved here usually means some kind of thread pooling is used in servers. This avoids having to destroy and create threads on the fly.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms686724(v=vs.85).aspx[
^]