Close the socket and release the connection on error