title: What could be the reason for a socket error "[Errno 9] Bad file descriptor"
body: <p>I have a complex python (2.7.3) script which is trying to open a socket connection via </p>
  
  <pre><code>self.socket.close()
  # doing something else
  self.socket.connect((host, port))
  </code></pre>
  
  <p>but all I get is the following socket error:</p>
  
  <pre><code>error: [Errno 9] Bad file descriptor
  </code></pre>
  
  <p>The <code>host:port</code> accepts connections as I have verified this with <code>nc host port</code> manually. So what could be the possible reasons I get this error for opening a connection to the given port, which actually works? </p>
  
  <p>I cannot and will not post the full script as it is too complex and irrelavent for this question. I just would like to know all possible reasons for this error, and how to check them and fix them. </p>