Fix close procedure in the net module

This improves the error messages in the net module. It also makes sure `SSL_shutdown` is called before closing the underlying socket. This is required for #1743 to function with https.

Requesting comments...