Fixing second socet exception.

The socket may get closed due to an error, in which case the close() in
the finally clause raises a second exception.