title: Address already in use when restarting socket
body: <p>I have the following Socket Server to run in a Raspberry.</p>
  <p>I can connect to it, send commands and it replies the command.</p>
  <p>Now I want that when I send the command <code>RESET</code> the Socket gets disconnected and the Raspberry returns to a Listening state, accepting a new connection.</p>
  <p>However it seems that the address is not properly liberated and the OS throws an &quot;Address already in use&quot; exception.</p>
  <pre><code>Reconnecting....
  OSError: [Errno 98] Address already in use
  
  
  import socket
  class SocketController:
      def __init__(self):
          self.s = None
          self.conn = None
          self.address = None
  
      def connect(self):
          host = ''
          port = 5560
          self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.s.bind((host, port))
          self.s.listen(1)
          self.conn, self.address = self.s.accept()
          
  
      def disconnect(self):
          self.conn.close()
          self.s.close()
  
      def rx(self):
          data = self.conn.recv(1024).decode('utf-8')
          if data == &quot;RESET&quot;:
              self.disconnect()
              print(&quot;Reconnecting....&quot;)
              self.connect()
          else:
              self.conn.sendall(str.encode(data))
  
  if __name__ == '__main__':
      s = SocketController()
      s.connect()
      while True:
          rxData = s.rx()
  </code></pre>
  <p>What is the proper way of closing the connection and getting back to listening?</p>