make sure connection is properly closed after having exception on socket

want to make sure the socket is properly closed when the exception happens. currently, if fwrite() returns FALSE, the connection isn't closed in postimpl.