title: Socket connection stays alive for some time even after closing
body: <p>Folks,</p>
  
  <p>My environment is Ubuntu 12.04.</p>
  
  <p>Here is a pseudo-code for my TCP server application that is listening for a connection:</p>
  
  <pre><code>while(true) {
      int hConn = accept(hMain, NULL, NULL);
  
      string s = readClient(hConn);
      if (s == "quit") {
          close(hConn);
      }
   }
  </code></pre>
  
  <p>While my server is running, I telnet to localhost at port nnnn:</p>
  
  <pre><code>$ telnet localhost nnnn
  quit
  Connection closed by foreign host.
  $
  </code></pre>
  
  <p>When the server receives "quit," it closes the client connection. This causes the telnet client to quit with an appropriate message.</p>
  
  <p>So far so good.</p>
  
  <p>However, when I run netstat, I can still see that the client connection is still alive.</p>
  
  <p>It takes a few seconds for the connection to disappear.</p>
  
  <p>This happens even if I force quit my server app.</p>
  
  <p>If I run my server app once again, I get an error that port "nnnn" is still in use.</p>
  
  <p>I have to wait for a few seconds before I can run my server app  once again.</p>
  
  <p>Is there something that I am missing? Is there a way to fix this behavior?</p>
  
  <p>Note that I am indeed closing socket hMain when quitting the server although this is not shown in the above pseudo-code.</p>
  
  <p>Thank you in advance for your help.</p>
  
  <p>Regards,<br>
  Peter</p>