Extracted the read and write to the socket to own function