def receive(sock):
    """Read all the data from a socket until it closes."""
    chunks = []
    while 1:
        chunk = sock.recv(4096)
        if chunk: chunks.append(chunk)
        else: return ''.join(chunks)