title: How can I reconnect a socket after a broken pipe?
body: <p>The program connects to a server, and when the connection is closed by the server, if I try to reconnect it says: <code>socket.error: [Errno 9] Bad file descriptor</code><br>
  If I close the socket in the client and then i try to reconnect, it says: <code>socket.error: [Errno 106] Transport endpoint is already connected</code>.</p>
  
  <hr>
  
  <p>Is there a way to reconnect it after a broken pipe without creating a new socket?</p>
  
  <pre><code>import socket
  host = '127.0.0.1'
  port = 1337
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  def loop():
      try:
          while 1:
              print s.recv(512)
      except socket.error:
          #s.close()?
          connect()
  def connect():
      s.connect((host, port))
      loop()
  connect()
  </code></pre>