Only try and close a socket if it's set, update the integration test