title: Python TCP/IP socket - Sending and receiving messages in order
body: <p>Happy new year everyone!</p>
  <p>I have created a socket TCP/IP using Python. In client side, I send the text &quot;A&quot; and &quot;B&quot;, and in the server side, if I receive the correct text (A &amp; B), I will send back to client a reply: &quot;GOOD&quot;.
  Everything is good until I change the order of the text to &quot;B&quot; and &quot;A&quot;.</p>
  <p>How can I improve the server side to receive the messages and don't care about the order A-B or B-A.</p>
  <p><strong>Server:</strong></p>
  <pre><code>import socket
  
  HOST = 'localhost'
  PORT = 8019
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.bind((HOST, PORT))
  s.listen(1) # Number of connections
  
  # Server loop
  while True:
      # Accept connections
      client, address = s.accept()
      print(&quot;Connected to&quot;, address)
  
      # Receive data and decode using utf-8
      data = client.recv( 1024 ).decode( 'utf-8' )
      print(&quot;Received :&quot;, repr(data))
  
      # Send data to client in utf-8
      if (&quot;A&quot; == data):
          reply = &quot;GOOD&quot;
          client.send(reply.encode('utf-8'))
      else:
          reply = &quot;FAIL&quot;
          client.send(reply.encode('utf-8'))
      # Receive data and decode using utf-8
      data = client.recv( 1024 ).decode( 'utf-8' )
      print(&quot;Received :&quot;, repr(data))
  
      # Send data to client in utf-8
      if (&quot;B&quot; == data):
          reply = &quot;GOOD&quot;
          client.send(reply.encode('utf-8'))
      else:
          reply = &quot;FAIL&quot;
          client.send(reply.encode('utf-8'))
      s.close()
      break
  </code></pre>
  <p><strong>Client:</strong></p>
  <pre><code>import socket
  
  # Create socket and connect it to server
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  s.connect(('localhost',8019))
  
  # client loop
  while True:
      message = &quot;A&quot;
      s.send(message.encode('utf-8'))
      print(&quot;Awaiting the reply...&quot;)
      reply = s.recv( 1024 ).decode( 'utf-8' )
      print(&quot;Received &quot;, str(reply))
      
      message = &quot;B&quot;
      s.send(message.encode('utf-8'))
      print(&quot;Awaiting the reply...&quot;)
      reply = s.recv( 1024 ).decode( 'utf-8' )
      print(&quot;Received &quot;, str(reply))
      s.close()
      break
  </code></pre>
  <p>Any help would be greatly appreciated! Best regards!</p>