Ciao a tutti!

Sono un po' di giorni che sto sbattendo la testa su un problema e non riesco a venirne a capo. Sto sviluppando un mail server multithread. Posto direttamente il codice (è molto lungo perciò ometterò alcune parti):

codice:
//MultiThreadedServer.c
static SOCKET POP3Socket = INVALID_SOCKET;
static SOCKET SMTPSocket = INVALID_SOCKET;

void MultiThreadedServer()  
{
...
setWinSocket();  // Imposta i socket SMTP e POP3
while(TRUE){
hSMTP=(HANDLE)_beginthreadex(NULL,0,SMTPThrdConn,&tArgs[iThd+1],0,&SMTPThId);
tStatus = WaitForSingleObject(hSMTP,INFINITE);
if(tStatus==WAIT_OBJECT_0){
    CloseHandle(hSMTP);
}
...
}

static DWORD WINAPI SMTPThrdConn(SRV_PARAMS *tArgs)
{
...
tArgs->SMTPSocket=accept(SMTPSocket,...);
tArgs->srv_hndl=(HANDLE)_beginthreadex (NULL, 0, SMTPCommands, tArgs, 0, &ThId);
...
}

// SMTPCommands.c
void SMTPCommands(SRV_PARAMS *tArgs)
{
...
char *tmpStr=(char *)calloc(MAX_REQ_SIZE,sizeof(char));
while (!session->Done && !flagsGuard.ReReadCfg){
	session->request=(char *)calloc(MAX_REQ_SIZE,sizeof(char));
	do{
		memset(tmpStr,0,sizeof(tmpStr));
		iResult=recv(tArgs->SMTPSocket,tmpStr,sizeof(tmpStr)-1,0);
		if(iResult<0)
			PrintError("recv() failed:");
		strcat(session->request,tmpStr);
	} while(strstr(tmpStr,"\n")==NULL);
...
}
Il problema è che la recv spesso ritorna -1 e il fatto ancora più strano è che GetLastError() ritorna 0. A schermo infatti mi stampa "recv() failed: Operazione completata".

Provando a fare un debugging con gdb al momento dell'errore digito bt e mi ritorna:
codice:
#0  SMTPCommands (tArgs=0x28fddc) at SMTPCommands.c:35 [dove controllo che iResult<0]
#1  0x77221287 in msvcrt!_itow_s () from C:\Windows\syswow64\msvcrt.dll
#2  0x77221328 in msvcrt!_endthreadex () from C:\Windows\syswow64\msvcrt.dll
#3  0x77123677 in KERNEL32!BasepMapModuleHandle ()
   from C:\Windows\syswow64\kernel32.dll
#4  0x005b3770 in ?? ()
#5  0x029bffd4 in ?? ()
#6  0x77969d72 in ntdll!RtlpNtMakeTemporaryKey ()
   from C:\Windows\system32\ntdll.dll
#7  0x005b3770 in ?? ()
#8  0x74145183 in ?? ()
#9  0x00000000 in ?? ()
Non riesco proprio a capire cos'è che non va.