        message = data.encode()
        with self._send_lock:
            self._socket.sendall(struct.pack('!I', len(message)))
            self._socket.sendall(message)

    def recv_msg(self) -> str:
        try:
            length, = struct.unpack('!I', self._recvall(4))
            return self._recvall(length).decode()
        except timeout:
            raise TimeoutError()

    def _recvall(self, count: int) -> bytes: