title: Error: Address already in use while binding socket with address but the port number is shown free by `netstat`
body: <p>I tried to bind my socket(server socket) at port number <code>8000</code>. It worked and did the job for me. At the end of the code I close the socket as well. The very next instant I run my code again and it shows me that the address is already in use. I have printed the meaning of error values <code>strerror(errno);</code> to see if my code working properly at each point. To check if the port is free I checked it using <code>netstat</code> but it shows that port number <code>8000</code> is free. It has happened with me a lot of times. Every time I then wait for a few more secs and then it starts working again. I am using c language. So what is he reason for this behavior by my OS.</p>
  
  <p>After a few more secs I run the code and then it works. 
  </p>
  
  <pre class="lang-none prettyprint-override"><code>anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out 
  Socket Creation: Success
  File open: Success
  Socket Bind: Address already in use
  Socket Listen: Address already in use
  ^C
  anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo netstat -lntp
  Active Internet connections (only servers)
  Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
  tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1348/lighttpd   
  tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      984/sshd        
  tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1131/cupsd      
  tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      1211/mysqld     
  tcp6       0      0 :::22                   :::*                    LISTEN      984/sshd        
  tcp6       0      0 ::1:631                 :::*                    LISTEN      1131/cupsd      
  anirudh@anirudh-Aspire-5920:~/Desktop/testing$ sudo ./a.out 
  Socket Creation: Success
  File open: Success
  Socket Bind: Address already in use
  Socket Listen: Address already in use
  ^C
  anirudh@anirudh-Aspire-5920:~/Desktop/testing$ 
  </code></pre>