protected override IAsyncResult OnBeginAcceptChannel(TimeSpan timeout, AsyncCallback callback, object state)
 {
     return(Tap.Run(callback, state, async() =>
     {
         try
         {
             var dataSocket = await listenSocket.AcceptAsync();
             return (IDuplexSessionChannel) new ServerTcpDuplexSessionChannel(this.encoderFactory, this.bufferManager, dataSocket, new EndpointAddress(this.Uri), this);
         }
         catch (SocketException ex) when(ex.SocketErrorCode == SocketError.OperationAborted)
         {
             return null;
         }
     }));
 }
        public IAsyncResult BeginSend(Message message, TimeSpan timeout, AsyncCallback callback, object state)
        {
            base.ThrowIfDisposedOrNotOpen();
            var encodedBytes = this.EncodeMessage(message);

            return(Tap.Run(callback, state, async() =>
            {
                try
                {
                    await WriteDataAsync(encodedBytes);
                }
                catch (SocketException socketException)
                {
                    throw ConvertSocketException(socketException, "Receive");
                }
            }));
        }
        protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            return(Tap.Run(callback, state, async() =>
            {
                var servers = await Hostname.ParseAsync(Via.Authority, 8081);
                for (int i = 0; i < servers.Length; i++)
                {
                    try
                    {
                        var address = servers[i];
                        var socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                        await socket.ConnectAsync(address);

                        base.InitializeSocket(socket);

                        break;
                    }
                    catch (SocketException socketException) when(i < servers.Length - 1)
                    {
                        throw ConvertSocketException(socketException, "Connect");
                    }
                }
            }));
        }
        public IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
        {
            base.ThrowIfDisposedOrNotOpen();

            return(Tap.Run(callback, state, () => ReceiveAsync()));
        }