Check the connection before closing