Use the MSG_NOSIGNAL flag where available to suppress SIGPIPE errors

Also, catch the EPIPE, ECONNRESET and ENOTCONN return conditions from
send and close the socket in any of these cases. This follows the C++
implementation.