There is a server that responds to requests on a socket. The client has functions to emit requests and functions to handle responses from the server.The problem is that the request sending function and the response handling function are two unrelated functions. Given a server response X, how can I know whether it's a response to request X or some other request Y?I would like to make a construct that would ensure that response X is definitely the answer to request X and also to make a function requestX() that returns response X and not some other response Y.This question is mostly about the general programming approach and not about any specific language construct. Preferably, though, the answer would involve Ruby, TCP sockets, and PHP.My code so far:require 'socket'class TheConnection    def initialize(config)        @config = config    end    def send(s)        toConsole(--> #{s})        @conn.send #{s}\n, 0     end    def connect()        # Connect to the server        begin            @conn = TCPSocket.open(@config['server'], @config['port'])        rescue Interrupt        rescue Exception => detail            toConsole('Exception: ' + detail.message())            print detail.backtrace.join('\n')            retry        end    end    def getSpecificAnswer(input)        send GET #{input}    end    def handle_server_input(s)        case s.strip            when /^Hello. (.*)$/i                toConsole [ Server says hello ]                send Hello to you too! #{$1}                          else                toConsole(s)        end    end    def main_loop()        while true            ready = select([@conn, $stdin], nil, nil, nil)            next if !ready            for s in ready[0]                if s == $stdin then                    return if $stdin.eof                    s = $stdin.gets                    send s                elsif s == @conn then                    return if @conn.eof                    s = @conn.gets                    handle_server_input(s)                end            end        end    end    def toConsole(msg)        t = Time.new        puts t.strftime([%H:%M:%S]) + ' ' + msg    endend@config = Hash[    'server'=>'test.server.com',    'port'=>'2020']$conn = TheConnection.new(@config)$conn.connect()$conn.getSpecificAnswer('itemsX')begin    $conn.main_loop()rescue Interruptrescue Exception => detail    $conn.toConsole('Exception: ' + detail.message())    print detail.backtrace.join('\n')    retryend
How to match responses from a server with their corresponding requests?
ruby;sockets