title: accept returns 0 for 2nd, 3rd, etc client
body: <p>I'm writting a simple socket server/client app.
  I run into interesting problem. In my server code I call accept on non-blocking socket like this</p>
  
  <pre><code>while ((res = accept(m_sd, NULL, 0)) &gt;= 0) { // There are new clients
    ... // Saving res as fd etc
  }
  </code></pre>
  
  <p>Everything works perfectly - when there is a client, accept returns a valid file descriptor. However when a first client disconnects and second client connect, accept returns 0 - which is a valid FD, howerver all operation on this descriptor fails. This happens also for the next clients - accept is returning 0. After random number of clients, acceptr returns a "valid" (non-zero) descritpor, and than it repeats.</p>
  
  <p>Note: When there are no clients, accept returns -1 as expected with errno EAGAIN - which is completly fine. When accept returns zero, errno is not set.</p>
  
  <p>What could cause such a weird behavior?</p>
  
  <p>Here's how I create server socket:</p>
  
  <pre><code>     struct sockaddr_in serv_addr;
       m_sd = socket(AF_INET, SOCK_STREAM, 0);
       if (m_sd &lt; 0){}
          //Handle error
  
       bzero((char *)&amp;serv_addr, sizeof(serv_addr));
       serv_addr.sin_family = AF_INET;
       serv_addr.sin_addr.s_addr = INADDR_ANY;
       serv_addr.sin_port = htons(port);
       int optval = 1;
       setsockopt(m_sd, SOL_SOCKET, SO_REUSEADDR, &amp;optval, sizeof optval);
  
       if (bind(m_sd, (struct sockaddr *)&amp;serv_addr, sizeof(serv_addr)) &lt; 0) {
           // Handle error
       }
       fcntl(m_sd, F_SETFL, O_NDELAY); // Make socket non-blocking
  
       listen(m_sd, 50);
  </code></pre>
  
  <p>And here's how I create client:</p>
  
  <pre><code>    int rc;
      struct sockaddr_in serveraddr;
      struct hostent *hostp;
      m_sd = socket(AF_INET, SOCK_STREAM, 0);
      if (m_sd &lt; 0)
             // Handle error
      memset(&amp;serveraddr, 0, sizeof(struct sockaddr_in));
      serveraddr.sin_family = AF_INET;
      serveraddr.sin_port = htons(port);
  
      hostp = gethostbyname(hostname.c_str());
      if (hostp == NULL)
        // Handle error
  
      memcpy(&amp;serveraddr.sin_addr, hostp-&gt;h_addr, sizeof(serveraddr.sin_addr));
  
      // connect to serveraddr
      rc = connect(m_sd, (struct sockaddr*)&amp;serveraddr, sizeof(serveraddr));
      if (rc &lt; 0)
        //Handle error
      //set to nonblocking
      fcntl(m_sd, F_SETFL, fcntl(m_sd, F_GETFL, 0) | O_NONBLOCK);
  </code></pre>
  
  <p>This is the code, where I wait for new data from any client:</p>
  
  <pre><code>    struct timeval tv;
  
      tv.tv_sec = 0;
      tv.tv_usec = std::chrono::duration_cast&lt;std::chrono::microseconds&gt;(timeout).count();
  
      fd_set rfds;
      FD_ZERO(&amp;rfds);
      FD_SET(m_sd, &amp;rfds);
      int end = m_sd;
      for (const auto&amp; s : m_clients) {
          end = std::max(end, s.second.m_sd);
          FD_SET(s.second.m_sd, &amp;rfds);
      }
  
      int retval = select(end + 1, &amp;rfds, NULL, NULL, &amp;tv);
      if (retval == -1) {
         // Error handling
      }
      return retval &gt; 0; // There is pending data from client
  </code></pre>