Repository: microsoft/garnet
File: libs/common/Networking/TcpNetworkHandlerBase.cs (C#)

Code before:
            var receiveEventArgs = new SocketAsyncEventArgs { AcceptSocket = socket };
            receiveEventArgs.SetBuffer(networkReceiveBuffer, 0, networkReceiveBuffer.Length);
            receiveEventArgs.Completed += useTLS ? RecvEventArgCompletedWithTLS : RecvEventArgCompletedWithoutTLS;

            // If the client already have packets, avoid handling it here on the handler so we don't block future accepts.
            try
            {
                if (!socket.ReceiveAsync(receiveEventArgs))
                {
                    if (useTLS)
                        Task.Run(() => RecvEventArgCompletedWithTLS(null, receiveEventArgs));
                    else
                        Task.Run(() => RecvEventArgCompletedWithoutTLS(null, receiveEventArgs));
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "An error occurred at Start.ReceiveAsync");
                Dispose(receiveEventArgs);
            }
        }

        /// <inheritdoc />
        public override void Dispose()
        {
            if (socket.Connected)
            {
                socket.Shutdown(SocketShutdown.Both);
                socket.Close();
            }

            socket.Dispose();
        }

        void Dispose(SocketAsyncEventArgs e)
        {
            e.AcceptSocket.Dispose();
            DisposeImpl();
            e.Dispose();
        }

        void RecvEventArgCompletedWithTLS(object sender, SocketAsyncEventArgs e) =>
            _ = HandleReceiveWithTLSAsync(sender, e);

        void RecvEventArgCompletedWithoutTLS(object sender, SocketAsyncEventArgs e) =>
            HandleReceiveWithoutTLS(sender, e);

        private void HandleReceiveWithoutTLS(object sender, SocketAsyncEventArgs e)
        {
            try
            {

Review comment (suggestion):
Try this:

```cs
        try
        {
            if (socket.Connected)
            {
                // Gracefully shutdown the socket connection
                socket.Shutdown(SocketShutdown.Both);
            }
        }
        catch (Exception ex)
        {
            logger?.LogError(ex, "Error shutting down socket");
        }
        finally
        {
            // Always close the socket to release the resources
            socket.Close();
            // Dispose of the socket to free up unmanaged resources
            socket.Dispose();
        }
    }
```

Code after:
            var receiveEventArgs = new SocketAsyncEventArgs { AcceptSocket = socket };
            receiveEventArgs.SetBuffer(networkReceiveBuffer, 0, networkReceiveBuffer.Length);
            receiveEventArgs.Completed += useTLS ? RecvEventArgCompletedWithTLS : RecvEventArgCompletedWithoutTLS;

            // If the client already have packets, avoid handling it here on the handler so we don't block future accepts.
            try
            {
                if (!socket.ReceiveAsync(receiveEventArgs))
                {
                    if (useTLS)
                        Task.Run(() => RecvEventArgCompletedWithTLS(null, receiveEventArgs));
                    else
                        Task.Run(() => RecvEventArgCompletedWithoutTLS(null, receiveEventArgs));
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "An error occurred at Start.ReceiveAsync");
                Dispose(receiveEventArgs);
            }
        }

        /// <inheritdoc />
        public override void Dispose()
        {
            try
            {
                if (socket.Connected)
                {
                    // Gracefully shutdown the socket connection
                    socket.Shutdown(SocketShutdown.Both);
                }
            }
            catch (Exception ex)
            {
                logger?.LogError(ex, "Error shutting down socket");
            }
            finally
            {
                // Always close the socket to release the resources
                socket.Close();
                // Dispose of the socket to free up unmanaged resources
                socket.Dispose();
            }
        }

        void Dispose(SocketAsyncEventArgs e)
        {
            e.AcceptSocket.Dispose();
            DisposeImpl();
            e.Dispose();