title: how to accept multiple clients with a simple winsock tcp server?
body: <p>I can't figure out how to accept multiple connections for a simple tcp server using winsock. </p>
  
  <p>I've tried a couple different ways and I can't figure out how to get another connection to work. The first call to accept() seems to be the only one that works. Can someone provide a simple example of how to do this or explain what's going on with listen() and accept() here?</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;Winsock2.h&gt;
  
  WSADATA WsaData; 
  unsigned char packet_in[64];
  unsigned short port = 29992;
  unsigned int max_packet_size =  sizeof(packet_in);
  sockaddr_in xaddress;
  SOCKET sock;
  SOCKET sock1 = INVALID_SOCKET;
  SOCKET sock2 = INVALID_SOCKET;
  
  int main()
  {   
  
      int r = WSAStartup( MAKEWORD(2,2),  &amp;WsaData );
      xaddress.sin_family = AF_INET; 
      xaddress.sin_port =  htons(port);
      DWORD nonBlocking = 1;
      sockaddr_in from; 
      int fromLength = sizeof( from );  
      int count = 0;
  
      sock = socket( AF_INET,  SOCK_STREAM,  IPPROTO_TCP );   
  
      r = bind(sock, (const sockaddr*) &amp;xaddress,  sizeof(sockaddr_in));
          if (r != 0) printf("%d\n", WSAGetLastError());
  
      r = listen(sock, SOMAXCONN);
      if (r == SOCKET_ERROR)
              printf("%d\n",WSAGetLastError());
  
      ioctlsocket(sock, FIONBIO, &amp;nonBlocking);
  
      while(true)
      {
  
          if(count == 0)
          {
              sock1 = accept(sock, NULL, NULL);
  
          }
          else if (count == 1)
          {
              sock2 = accept(sock, NULL, NULL);
  
          }
  
          if(sock1 != INVALID_SOCKET)
              count = 1;
          if(sock2 != INVALID_SOCKET)
              count = 2;
          printf("%i\n",count);
  
  
      }
      return 0;
  }
  </code></pre>