title: Using sockets and threading, my clients won't stay connected
body: <p>I have the following code for my server loop:</p>
  
  <pre><code>    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
      server.bind((host, port))
      while True:
          server.listen(1)
          clientsock, clientAddress = server.accept()
          self.total_clients += 1
          self.modules['logger'].log("login", "New connection! =&gt; " + str(clientAddress))
          self.clients[self.total_clients] = (Client(clientAddress, clientsock, self.total_clients, self.modules))
          self.clients[self.total_clients].start()
  </code></pre>
  
  <p>Basically someone connects to the socket, and after that they're placed in that loop on the 2nd to last line (the one which instantiates the Client class). The Client class is as follows:</p>
  
  <pre><code>class Client(threading.Thread):
      def __init__(self, addr, clientsocket, thread_id, modules):
          threading.Thread.__init__(self)
  
          self.csocket = clientsocket
          self.addr = addr
          self.thread_id = thread_id
          self.modules = modules
  
          return
  
      def run(self):
          while True:
              data = self.csocket.recv(2048)
              if(len(data) &gt; 1):
                  data = data.decode()
                  if data == 'bye':
                      break
                  self.clientLog("Data received: {" + data + "}")
                  self.parseCatch(self.modules['parser'].parse(data))
          print ("Client ", clientAddress , " disconnected...")
  </code></pre>
  
  <p>So basically I want the program to work like this: A client connects to the socket. They are then placed into the run() loop in the Client class-thread indefinitely for anything else they send, and the loop responds to certain packets (this is done in the parseCatch() function which I have not included as it is just a simple "if x received send y back").</p>
  
  <p>Anyways, the program isn't functioning like that. As it works now, a client connects to the socket and is placed into the run() loop in the Client class-thread. This is good so far. They then send a certain packet, lets say they send "hi". This is received in 'data' in the run loop in the bottom Client class, and let's say I respond back to hi with <code>self.csocket.send('hi back');</code>. This works fine. Their client receives the hi back. However, if they send something back, like bye, it is treated as a brand new client. </p>
  
  <p>So somehow, when this arbitrary client replies to my packet I sent in the Client() thread, they are treated as a brand new connection. So if client sends "hi", server sends "hi back", it is so far so good. However, if a client then responds again and says something like "bye", it is somehow handled by the main server loop in the top block of code, and I get the "New connection! =>" message. I do not want the client to be handled in this loop, I want them to be handled in their own preexisting thread. </p>
  
  <p>For some reason when a client replies with new data they exit their thread. The loop appears to not even be broken, because it never says "Client disconnected".</p>
  
  <p>Please help. Thanks.</p>