title: bind() fails occasionally with Address already in use even after setting SO_REUSEADDR and SO_REUSEPORT
body: <p>We have a streaming server implemented using live555 library. This server is deployed on CentOS instance.</p>
  <p>Recently we wanted to modify the server socket options, so that it can accept the requests immediately after re-starting the process (either after crash or manual restart).</p>
  <p>I have referred to man pages and few web links and set the socket options (<code>SO_REUSEADDR</code> and <code>SO_REUSEPORT</code>) before <code>bind()</code> call.</p>
  <pre><code>int setupStreamSocket(UsageEnvironment&amp; env,
                        Port port, Boolean makeNonBlocking) {
  
  
    int newSocket = createSocket(SOCK_STREAM);
    if (newSocket &lt; 0) {
      socketErr(env, &quot;unable to create stream socket: &quot;);
      return newSocket;
    }
  
    int reuseFlag = 1;
    fprintf(stderr,&quot;reuseFlag : %d\n&quot;,reuseFlag);
  
    if (setsockopt(newSocket, SOL_SOCKET, SO_REUSEADDR,
                   (const char*)&amp;reuseFlag, sizeof reuseFlag) &lt; 0) {
      socketErr(env, &quot;setsockopt(SO_REUSEADDR) error: &quot;);
      closeSocket(newSocket);
      return -1;
    }
  
    if (setsockopt(newSocket, SOL_SOCKET, SO_REUSEPORT,
                   (const char*)&amp;reuseFlag, sizeof reuseFlag) &lt; 0) {
      socketErr(env, &quot;setsockopt(SO_REUSEPORT) error: &quot;);
      closeSocket(newSocket);
      return -1;
    }
    int flag = 1;
    setsockopt( newSocket, IPPROTO_TCP, TCP_NODELAY, &amp;flag, sizeof(flag) );
  
    if (port.num() != 0 || ReceivingInterfaceAddr != INADDR_ANY) {
  
      MAKE_SOCKADDR_IN(name, ReceivingInterfaceAddr, port.num());
      int reuse_addr_val, reuse_port_val;
      socklen_t reuse_addr_len = sizeof(reuse_addr_val);
      socklen_t reuse_port_len = sizeof(reuse_port_val);
      getsockopt(newSocket, SOL_SOCKET, SO_REUSEADDR, &amp;reuse_addr_val, &amp;reuse_addr_len);
      getsockopt(newSocket, SOL_SOCKET, SO_REUSEPORT, &amp;reuse_port_val, &amp;reuse_port_len);
      fprintf(stderr,&quot;reuse_addr_val = %d, reuse_port_val = %d\n&quot;, reuse_addr_val, reuse_port_val);
  
      if (bind(newSocket, (struct sockaddr*)&amp;name, sizeof name) != 0) {
        char tmpBuffer[100];
        sprintf(tmpBuffer, &quot;bind() error (port number: %d): &quot;,
                ntohs(port.num()));
        socketErr(env, tmpBuffer);
        closeSocket(newSocket);
        return -1;
      }
    }
  
    if (makeNonBlocking) {
      if (!makeSocketNonBlocking(newSocket)) {
        socketErr(env, &quot;failed to make non-blocking: &quot;);
        closeSocket(newSocket);
        return -1;
      }
    }
  
    return newSocket;
  }
  </code></pre>
  <p>This code works as expected (binding to the address even when a socket is in <code>TIME_WAIT</code> state) if I restart the server with above options.</p>
  <p>If I replace previous build (without socket options) with the build created with above code then I have noticed that occasionally <code>bind()</code> is failing with Address already in use error.</p>
  <p>When the <code>bind()</code> failed the port/address is in <code>TIME_WAIT</code> state. So the server should able to bind the socket to the address.</p>
  <blockquote>
  <p>tcp        0      0 10.0.1.24:8554              10.0.1.89:27085       TIME_WAIT   -</p>
  </blockquote>
  <p>And the <code>getsockopt()</code> of my code printed the flag value (corresponding to <code>SO_REUSEADDR</code> and <code>SO_REUSEPORT</code>) as 1.</p>
  <blockquote>
  <p>reuse_addr_val = 1, reuse_port_val = 1</p>
  <p>bind() error (port number: 8554): Address already in use</p>
  </blockquote>
  <p>So I'm wondering why it's failing only few times. Did I miss something in my code ? or is this an expected behavior ?</p>