Under macOS, for example, If the socket for whatever reason is closed, you end up in the SIGPIPE handler.
Setting the SO_NOSIGPIPE option will instruct the OS to not generate SIGPIPE, but instead return EPIPE, which can be handled gracefully along with other IO errors.