title: Accept call does'nt block the second time (or more than one time)?
body: <p>I have the following code and I`m not sure why wont it work properly .</p>
  
  <p>Its a multithreaded TCP server that loops accept() calls and triggers a designated</p>
  
  <p>thread each time .</p>
  
  <p>The problem is that the accept at times wont block , thus resulting</p>
  
  <p>the program to open a new thread when there's theoretically no connection .</p>
  
  <p>That's the loop -</p>
  
  <pre><code>    for (dwI = 0;; dwI++)                       //Accept MAX_CLIENTS connections
  {
      if(MAX_CLIENTS == dwI)
      {
          dwI=0;
          continue;
      }//if
  
      if(clients[dwI].bIsInUse)
      {
          continue;
      }//if
  
      ZeroMemory(&amp;from,sizeof(from));
  
      if(!AcceptConnection(&amp;ServerSock,&amp;from,&amp;ClientSock))
      {
          PRINT_LE("AcceptConnection",ERROR_ACCEPT_SERVER_CONNECTION);
          closesocket(ServerSock);
          WSACleanup();
          return EXIT_FAILURE;
      }//if
  
      clients[dwI].ClientSock = ClientSock;
  
      if(! (clients[dwI].hThread = CreateThread(
              NULL,               //Not inheritable
              0,                  //Default stack size
              ThreadedAcceptTCP,       //ThreadedAccept - function
              &amp;clients[dwI],//Pass pointer to the socket
              0,                  //Start immidiately
              &amp;clients[dwI].dwThreadId                //Save thread id
              )))
      {
          PRINT_GLE("CreateThread");
          closesocket(ServerSock);
          WSACleanup();
          return EXIT_FAILURE;
      }//if
  
      #ifdef PRINT_STATUS                     //Print status if macro is defined
          printf("Server responce message has been sent.\n");
      #endif
  }//for
  </code></pre>
  
  <p>With my own wrappers to each function .</p>
  
  <p>AcceptConnection has the code below -</p>
  
  <pre><code>    SOCKET ClientSocket = INVALID_SOCKET;       //Client socket
  INT sockaddrSize = sizeof(*pSockAddr);
  
  ClientSocket = accept(          //Create client accepting socket
                  *pSock,         //Listen-ed socket
                  pSockAddr,      
                  &amp;sockaddrSize   
                  );
  
  if (INVALID_SOCKET == ClientSocket)     //Check for errors - if any - cleanup and return failure
  {
      PRINT_WSAGLE("socket");
      return FAILURE;
  }//if
  
  *pClientSock = ClientSocket;        //Pass socket
  
  return SUCCESS;
  </code></pre>
  
  <p>The problem occurs when I connect to the server through my browser , </p>
  
  <p>for example , </p>
  
  <p>after the first thread is done (I`ve made this sure by temporarily sleeping the main thread for 5 seconds) </p>
  
  <p>it cleans everything and closes the client socket ,</p>
  
  <p>though on the second accept call - it will return with the same </p>
  
  <p>SOCKADDR information and cause an extra thread to go up ,</p>
  
  <p>receiving the exact same data , sending the exact same data .</p>
  
  <p>And printing 2 (and at times even more) times :</p>
  
  <p>"Server responce message has been sent."</p>
  
  <p>I could'nt figure out why this happens and hopefully you guys could help me out .</p>
  
  <p>Thanks !</p>