title: FIN_WAIT_2 from my TCP code prevents opening socket
body: <p>I'm writing, running, debugging a TCP server on FreeBSD that my clients connect to.  When the server crashes or comes down somewhat ungracefully, often I have issues where I can't bring the server back up because my bind() call fails.  When this happens of course I can find the following in netstat -n:</p>
  
  <pre><code>Active Internet connections
  Proto Recv-Q Send-Q  Local Address          Foreign Address       (state)
  tcp4       0      0 192.168.2.xxx.12345    xx.yy.zz.ww.54201      FIN_WAIT_2
  </code></pre>
  
  <p>This is pretty irritating as I sit here for a minute or 2 or 3 until it clears out.  I have tried my best to avoid this:</p>
  
  <pre><code>if((socketId = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) &lt; 0) ...
  
  // Set socket to nonblocking.  If this is &lt; 0 there was a failure
  if(fcntl(socketId, F_SETFL, O_NONBLOCK) &lt; 0) ...
  
  
  // Linger settings
  struct linger so_linger;
  so_linger.l_onoff = 1; // Turn linger option TRUE!
  so_linger.l_linger = 0; // And inform to DO NOT LINGER
  
  
  if(setsockopt(socketId, SOL_SOCKET, SO_LINGER, &amp;so_linger, sizeof(so_linger)) &lt; 0) ...
  </code></pre>
  
  <p>So, why am I getting the lingering behavior?  This is on FreeBSD but I had the same issue on Linux before I ported my code here. I have tried to put in code so that when I "control-c" kill my server it should run a close on all these sockets and immediately exit.  That did not seem to help</p>