Dispose() public method

Dispose the current chain.
public Dispose ( ) : void
return void
        public DebugPortWorker(WorkerChain chain, IPEndPoint client, IPEndPoint server)
        {
            Chain = chain;
            socket = ((TcpReceiver)Chain[0]).Socket;
            base.ClientEndPoint = client;
            base.ServerEndPoint = server;

            sendStream = new BufferedStream(new NetworkStream(socket));

            try
            {
                lock (sendStream)
                {
                    Send((int)DebugDataType.GW_NAME);
                    Send(Chain.Gateway.Configuration.GatewayName);

                    Send((int)DebugDataType.FULL_IOC);
                    Send(Chain.Gateway.KnownIocs.Count);
                    foreach (var i in Chain.Gateway.KnownIocs)
                    {
                        Send(i.Key);
                        Send(i.Value.Count);
                        foreach (var j in i.Value)
                        {
                            Send(j);
                        }
                    }

                    Send((int)DebugDataType.FULL_CLIENT);
                    Send(Chain.Gateway.KnownClients.Count);
                    foreach (var i in Chain.Gateway.KnownClients)
                    {
                        Send(i.Key);
                        Send(i.Value.Count);
                        foreach (var j in i.Value)
                        {
                            Send(j);
                        }
                    }

                    if (PBCaGw.Services.DebugTraceListener.TraceAll)
                        Send((int)DebugDataType.FULL_LOGS);
                    else
                        Send((int)DebugDataType.CRITICAL_LOGS);
                    Flush();

                    PBCaGw.Services.LogEntry[] logs = PBCaGw.Services.DebugTraceListener.LastEntries;
                    foreach (var i in logs)
                    {
                        Send((int)DebugDataType.LOG);
                        Send(i.Source);
                        Send((int)i.EventType);
                        Send(i.Id);
                        Send(i.Message);
                        Flush();
                    }
                }
            }
            catch
            {
                Chain.Dispose();
            }

            Chain.Gateway.NewIocChannel += new NewIocChannelDelegate(GatewayNewIocChannel);
            Chain.Gateway.DropIoc += new DropIocDelegate(GatewayDropIoc);
            Chain.Gateway.NewClientChannel += new NewClientChannelDelegate(GatewayNewClientChannel);
            Chain.Gateway.DropClient += new DropClientDelegate(GatewayDropClient);

            PBCaGw.Services.DebugTraceListener.LogEntry += GatewayLogEntry;
            PBCaGw.Services.DebugTraceListener.TraceLevelChanged += new System.EventHandler(DebugTraceListenerTraceLevelChanged);
        }
        /// <summary>
        /// Removes an IOC (response) chain
        /// </summary>
        /// <param name="chain"></param>
        internal static void DropServerConnection(WorkerChain chain)
        {
            if (chain == null || chain[0] as TcpReceiver == null || ((TcpReceiver)chain[0]).Socket == null)
                return;

            IPEndPoint endPoint;
            try
            {
                endPoint = ((TcpReceiver)chain[0]).RemoteEndPoint;
            }
            catch
            {
                return;
            }

            Socket toDisconnect = null;
            lock (iocConnections)
            {
                if (!iocConnections.ContainsKey(endPoint))
                    return;

                if (Log.WillDisplay(System.Diagnostics.TraceEventType.Stop))
                    Log.TraceEvent(System.Diagnostics.TraceEventType.Stop, iocChains[endPoint].ChainId, "Disposing IOC chain " + endPoint);

                toDisconnect = iocConnections[endPoint];

                iocConnections.Remove(endPoint);
                iocChains.Remove(endPoint);
            }

            try
            {
                toDisconnect.Disconnect(false);
            }
            catch
            {
            }

            // Cleanup which shall not be usefull but somehow we get wrong data..
            // It's a work around not the real fix sadly.
            List<string> toCleanup = InfoService.ChannelEndPoint.OfType<KeyValuePair<string, Record>>()
                .Where(row => row.Value.Destination != null
                    && row.Value.Destination.ToString() == endPoint.ToString())
                .Select(row => row.Key).ToList();

            foreach (var i in toCleanup)
                InfoService.ChannelEndPoint.Remove(i);

            chain.Gateway.DoDropIoc(endPoint.ToString());
            chain.Dispose();
        }