minor fixes and handle SIGPIPE (which is sent when writing to an already closed socket)