Ensure socket is cleaned up on error.

In case of timeout, etc, the socket needs to be manually cleaned up.