internal void Attach(TransportPipe pipe)
 {
     _commands.Add(() =>
     {
         var socketAsyncEventArgs = new SocketAsyncEventArgs();
         var sendingPipeInfo = new SendingPipeInfo(pipe, socketAsyncEventArgs);
         socketAsyncEventArgs.UserToken = sendingPipeInfo;
         socketAsyncEventArgs.Completed += OnSendCompleted;
         _transportPipes.Add(sendingPipeInfo);
         CreateSocketForEndpoint(sendingPipeInfo);
     });
 }
        private void SendData(SendingPipeInfo pipe, IList<ArraySegment<byte>> data, int dataSize)
        {
            int sentBytes = 0;
            var socket = pipe.Socket;
            try
            {
                if (socket == null) //socket has been previously disconnected or cannot be created somehow, circuit breaker dont do anything
                {
                    //SaveUnsentData(pipe, data);
                    return;
                }
                pipe.EventArgs.BufferList = data;
                pipe.IsSending = true;
                if (!socket.SendAsync(pipe.EventArgs))
                    OnSendCompleted(null, pipe.EventArgs);
              //  CheckError(sentBytes, dataSize, socket);
                //     pipe.MessageContainerConcurrentQueue.FlushMessages(data);
            }

            catch (SocketException e)
            {
                _logger.Error(string.Format("Error on send {0}", e.Message));
                if (socket != null)
                {
                    socket.Disconnect(false);
                    socket.Dispose();
                }

                pipe.Socket = null;
                _timers.Add(_watch.ElapsedTicks + TimeSpan.FromSeconds(1).Ticks, () => CreateSocketForEndpoint(pipe));
                // SaveUnsentData(pipe, data);
            }
        }
        private Socket CreateSocketForEndpoint(SendingPipeInfo pipe)
        {
            try
            {
                _logger.Info(string.Format("Creating send socket for endpoint {0}", pipe.Pipe.EndPoint));
                var socket = pipe.Pipe.CreateSocket();
                if (!_transportPipes.Contains(pipe)) //dont add again in case it was detached
                    return null;
                pipe.Socket = socket;

                if (socket == null)
                {
                    _timers.Add(_watch.ElapsedTicks + TimeSpan.FromSeconds(1).Ticks, () => CreateSocketForEndpoint(pipe));
                    return null;
                }
                return socket;
            }
            catch (Exception e)
            {
                _logger.Error(string.Format("Error when creating socket for endpoint {0}, mess = {1}", pipe.Pipe.EndPoint, e.Message));
                _timers.Add(_watch.ElapsedTicks + TimeSpan.FromSeconds(1).Ticks, () => CreateSocketForEndpoint(pipe));
                return null;
            }
        }