I think the server is expecting newline at the end of the message. Try to use your original code with write() and add \n at the end to confirm this.
write()
\n