Hi everybody!I'm trying to make a simple p2p chat using winsock and winthreads but I can't get it to work:it seems to block on receive,receiving bytes even when socket is empty leading to a receive error!I list you my project files:

class.h
Code:
#include <winsock2.h>
#include <iostream>

using namespace std;

class chatsock{
      public:
             chatsock();
             chatsock(int port1);
             ~chatsock();
             void connection(char address[],int port1);
             void chat();
             void listening();
             void receiving();
             SOCKET getsock();
             
      private:
            SOCKET sock,guestsock;
            WORD p1;
            WSADATA data1;
            sockaddr_in internal_address;
            sockaddr_in destination_address;
            sockaddr_in guest_address;

            char buf[300];
            char recbuf[300];
            int n;
};
class.cpp

Code:
#include "class.h"
chatsock::chatsock(){
                     p1=MAKEWORD(2,2);
                               if(WSAStartup(p1,&data1)!=0){
                               cerr<<"WSA failed\n";
                               exit(1);
                               }
                               
             if((sock=socket(PF_INET,SOCK_STREAM,0))<0){
             cerr<<"Failed creating socket\n";
             exit(1);
             }
             
             internal_address.sin_family=AF_INET;
             internal_address.sin_port=htons((u_short)1234);
             internal_address.sin_addr.s_addr=htonl(INADDR_ANY);
    
             if(bind(sock,(struct sockaddr*)&internal_address,sizeof(internal_address))<0){
             cerr<<"Bind failed\n";
             exit(1);
             }
}
                     
chatsock::chatsock(int port1){
                          
                               p1=MAKEWORD(2,2);
                               if(WSAStartup(p1,&data1)!=0){
                               cerr<<"WSA failed\n";
                               exit(1);
                               }
                               
             if((sock=socket(PF_INET,SOCK_STREAM,0))<0){
             cerr<<"Failed creating socket\n";
             exit(1);
             }
             
             
             internal_address.sin_family=AF_INET;
             internal_address.sin_port=htons((u_short)port1);
             internal_address.sin_addr.s_addr=htonl(INADDR_ANY);
    
             if(bind(sock,(struct sockaddr*)&indirizzo_interno,sizeof(indirizzo_interno))<0){
             cerr<<"Bind failed\n";
             exit(1);
             }
}

void chatsock::connection(char address[],int port1){
    destination_address.sin_family=AF_INET;
    struct hostent* punthost;
    punthost=gethostbyname(address);
    destination_address.sin_addr.s_addr=*((unsigned long*)punthost->h_addr); 
    destination_address.sin_port=htons((u_short)port1);
    
    if(connect(sock,(struct sockaddr*)&destination_address,sizeof(destination_address))<0){
     cerr<<"Connect failed\n";
     exit(1);
    }
}

void chatsock::chat(){
     do{
    cout<<"Type>";
    gets(buf);
    send(sock,buf,sizeof(buf),0);
    }while(strcmp(buf,"quit")!=0);
     
}

void chatsock::listening(){
     if(listen(sock,1)<0){
     cerr<<"Listen failed\n";
     exit(1);
     }
     
     cout<<"Listening...\n";
     
    int alen=sizeof(guest_address);
    if((guestsock=accept(sock,(struct sockaddr*)&guest_address,&alen))<0){
     cerr<<"Accept fallito\n";
     exit(1);
     }
    cout<<"Someone has just connected\n";
    
}

void chatsock::receiving(){
     
     while(1){
             if(n=recv(sock,recbuf,sizeof(recbuf),0)<0){
             cerr<<"Receiving error\n";
             exit(1);
             }
             
             if(strcmp(recbuf,"quit")==0){
             cout<<"Client closed connection\n";
             break;
             }
             else
             cout<<"Received: "<<recbuf<<endl;
             }
}

SOCKET chatsock::getsock(){
       return sock;
}

chatsock::~chatsock(){
                      shutdown(sock,0);
                      closesocket(sock);
                      WSACleanup();
                      }
main

Code:
#include <iostream>
#include <cstdlib>
#include <process.h>
#include "class.h"

using namespace std;

DWORD WINAPI recthread(LPVOID lpparam);

int main(int argc, char *argv[])
{
    HANDLE threads;
    int choose;
    char IP[20];
    int port;
    chatsock* c1;
    
    if(argc>1)
    c1=new chatsock(atoi(argv[1]));
    else
    c1=new chatsock();
    
    cout<<"Connect(1) or listen(2)?\n";
    do{
    cout<<">";
    cin>>choose;
    switch(choose){
                   case 1:
                        cout<<"Type destination IP \n>";
                        cin>>IP;
                        cout<<"\nType port\n>";
                        cin>>port;
                        c1->connessione(IP,port);
                        threads=CreateThread(NULL,0,recthread,(void*)(c1),0,NULL);
                        c1->chat();
                        break;
                   case 2:
                        c1->listening();
                        threads=CreateThread(NULL,0,recthread,(void*)(c1),0,NULL);
         
                        c1->chat();
                        break;
                   default:
                        cout<<"Command not accepted\n";
                   }
    }while((scelta!=1)&&(scelta!=2));
    
    
    
    
    delete c1;
    system("PAUSE");
    return EXIT_SUCCESS;
}

DWORD WINAPI recthread(LPVOID lpparam){
      chatsock* ricezione=(chatsock*)lpparam;
      ricezione->ricezione();
      
      return 0;
}