Added code to make sure we don't try to close already-closed connections.