Fixed reading and writing from the socket