tls: handle errors on socket before releasing it

Fix sudden uncatchable ECONNRESETs, when using https server.