public PXAgent(string address, int port, IResolverContext context, IEnumerable <IPXStreamWrapper> wrappers, Func <IPXProtocol> protocolProvider, int senderId) { var clientId = Guid.NewGuid(); async void Connect() { var stream = WrapStream(new TcpClientAttachedStream(new TcpClient(address, port)), wrappers); await(new PXLLProtocol()).WelcomeFromSender(stream, clientId.ToString()); this.client.SetupStream(stream); clientReadyTSC.SetResult(this.client); } this.SenderId = senderId; this.client = new PXSocketClient(clientId.ToString(), context, protocolProvider); Connect(); this.client.OnDisconnected += aClient => { clientReadyTSC = new TaskCompletionSource <PXSocketClient>(); Connect(); }; context.SenderDispatcher().Register(this); }