private static void CleanupSocketEnd(TSocket tsocket) { var bothClosed = tsocket.CloseEnd(); if (bothClosed) { // First remove from the Dictionary, so we can't match with a new fd. tsocket.ThreadContext.RemoveSocket(tsocket.Fd); // We are not using SafeHandles to increase performance. // We get here when both reading and writing has stopped // so we are sure this is the last use of the Socket. tsocket.Socket.Dispose(); tsocket.DupSocket?.Dispose(); } }