Rewrite ssl_write() function based on fetchmail's SockWrite.

This version is more robust.

Signed-off-by: Thiago Farina <tfarina@chromium.org>