private void ReleaseCachedConnection(NetworkSender sender) { lock (this.currentSenderCache) { lock (this.openNetworkSenders) { if (this.openNetworkSenders.Remove(sender)) { // only remove it once sender.Close(ex => { }); } } NetworkSender sender2; // make sure the current sender for this address is the one we want to remove if (this.currentSenderCache.TryGetValue(sender.Address, out sender2)) { if (ReferenceEquals(sender, sender2)) { this.currentSenderCache.Remove(sender.Address); } } } }