title: golang errors with bind address already in use even though nothing is running on the port
body: <p>I have a setup in golang which basically gets a free port from OS and then starts a http sever on it. It started to give random errors with port signup failures. I simplified it into the following program which seems to error after grabbing a few free ports. It happens very randomly and there is no real process running on the port it errors. Doesn't make sense at all to me on why this has to error. Any help would be appreciated.</p>
  
  <p>Output of the program:</p>
  
  <p>..<br/>
  ..<br/>
  58479<br/>
  ..<br/>
  ..<br/>
  58867<br/>
  58868<br/>
  58869<br/>
  ..<br/>
  bound well!  58867<br/>
  bound well!  58868<br/>
  bound well!  58869<br/>
  ..<br/>
  ..<br/>
  ..<br/>
  2015/04/28 09:05:09 Error while binding port: listen tcp :58479: bind: address already in use<br/></p>
  
  <p>I made sure to check that the free port that came out never repeated.</p>
  
  <pre><code>package main
  
   import (
      "net"
      "net/http"
      "log"
      "fmt"
  )
  
  func main() {
  
      for {
          l, _ := net.Listen("tcp", ":0")
          var port = l.Addr().String()[5:]
          l.Close()
          fmt.Println(port)
          go func() {
                  l1, err := net.Listen("tcp", ":"+port)
                  if (err != nil) {
                      log.Fatal("Error while binding port: ", err.Error())
                  } else {
                      fmt.Println("bound well! ", port)
                  }
                  http.Serve(l1, nil)
              }()
      }
  }
  </code></pre>