public void ReadRequestAndWriteResponse()
        {
            IPAddress[] addressList = Dns.GetHostEntry(Environment.MachineName).AddressList;
            var endpoint = new IPEndPoint(addressList[addressList.Length - 1], 9900);
            var listener = new TcpListener(endpoint);
            listener.Start();

            var t = new Thread(new ThreadStart(() =>
            {
                var c = listener.AcceptTcpClient();
                var handle = new TcpProtocolHandle(c.GetStream());

                //read remoting request
                Console.WriteLine("Preamble: {0}", handle.ReadPreamble());
                Console.WriteLine("MajorVersion: {0}", handle.ReadMajorVersion());
                Console.WriteLine("MinorVersion: {0}", handle.ReadMinorVersion());
                var op = handle.ReadOperation();
                Assert.AreEqual(TcpOperations.Request, op);
                Console.WriteLine("Operation: {0}", op);
                Console.WriteLine("ContentDelimiter: {0}", handle.ReadContentDelimiter());
                Console.WriteLine("ContentLength: {0}", handle.ReadContentLength());
                DumpHelper.DumpDictionary(handle.ReadTransportHeaders());
                var messageRequest = BinaryFormatterHelper.DeserializeObject(handle.ReadContent()) as MethodCall;
                Assert.IsNotNull(messageRequest);
                DumpHelper.DumpMessage(messageRequest);

                //write remoting response
                var responeMessage = new MethodResponse(new Header[] { new Header(MessageHeader.Return, messageRequest.Args[0]) }, messageRequest);
                var responseStream = BinaryFormatterHelper.SerializeObject(responeMessage);
                handle.WritePreamble();
                handle.WriteMajorVersion();
                handle.WriteMinorVersion();
                handle.WriteOperation(TcpOperations.Reply);
                handle.WriteContentDelimiter(TcpContentDelimiter.ContentLength);
                handle.WriteContentLength(responseStream.Length);
                handle.WriteTransportHeaders(null);
                handle.WriteContent(responseStream);
            }));
            t.Start();

            var url = string.Format("tcp://{0}/remote.rem", endpoint);
            var service = RemotingServices.Connect(typeof(ServiceClass), url) as ServiceClass;
            Assert.AreEqual("Hi", service.Do("Hi"));

            t.Abort();
        }
        public void WriteRequestAndReadResponse()
        {
            var channel = new TcpChannel(8080);
            ChannelServices.RegisterChannel(channel, false);
            RemotingServices.Marshal(new ServiceClass(), "Remote");

            var uri = "tcp://localhost:8080/Remote";

            using (var client = new TcpClient())
            {
                client.Connect("localhost", 8080);
                using (var stream = client.GetStream())
                {
                    var messageRequest = new MethodCall(new Header[] {
                        new Header(MessageHeader.Uri, uri),
                        new Header(MessageHeader.MethodName, "Do"),
                        new Header(MessageHeader.MethodSignature, new Type[] { typeof(string) }),
                        new Header(MessageHeader.TypeName, typeof(ServiceClass).AssemblyQualifiedName),
                        new Header(MessageHeader.Args, new object[] { "Hi" })
                    });
                    var messageRequestStream = BinaryFormatterHelper.SerializeObject(messageRequest);

                    var handle = new TcpProtocolHandle(stream);

                    //send remoting request
                    handle.WritePreamble();
                    handle.WriteMajorVersion();
                    handle.WriteMinorVersion();
                    handle.WriteOperation(TcpOperations.Request);
                    handle.WriteContentDelimiter(TcpContentDelimiter.ContentLength);
                    handle.WriteContentLength(messageRequestStream.Length);
                    handle.WriteTransportHeaders(new Dictionary<string, object>() { { TcpTransportHeader.RequestUri, uri } });
                    handle.WriteContent(messageRequestStream);

                    //read remoting response
                    Console.WriteLine("Preamble: {0}", handle.ReadPreamble());
                    Console.WriteLine("MajorVersion: {0}", handle.ReadMajorVersion());
                    Console.WriteLine("MinorVersion: {0}", handle.ReadMinorVersion());
                    var op = handle.ReadOperation();
                    Assert.AreEqual(TcpOperations.Reply, op);
                    Console.WriteLine("Operation: {0}", op);
                    Console.WriteLine("ContentDelimiter: {0}", handle.ReadContentDelimiter());
                    Console.WriteLine("ContentLength: {0}", handle.ReadContentLength());
                    handle.ReadTransportHeaders();
                    var messageResponse = BinaryFormatterHelper.DeserializeObject(handle.ReadContent()) as MethodResponse;
                    Assert.IsNotNull(messageResponse);
                    DumpHelper.DumpMessage(messageResponse);
                    if (messageResponse.Exception != null)
                        throw messageResponse.Exception;
                    Assert.AreEqual("Hi", messageResponse.ReturnValue);
                }
            }
        }