add guard when writing to the socket