private void ProcessNPClient(NamedPipeServerStream pipeServer, CancellationToken cancellationToken)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                byte[] arrHeader = null;

                try
                {
                    while (pipeServer.IsConnected && (!cancellationToken.IsCancellationRequested))
                    {
                        // Fetch the header from the stream
                        arrHeader = new byte[GatewayPayloadConst.payloadHeaderSize];
                        int iTotalRead = FetchDataFromTransport(null, pipeServer, arrHeader, GatewayPayloadConst.payloadHeaderSize);
                        if (iTotalRead > 0)
                        {
                            // Did we get a buffer of expected size?
                            if (iTotalRead != GatewayPayloadConst.payloadHeaderSize)
                            {
                                throw new InvalidOperationException(String.Format("SFBlockstoreService::ProcessNPClient: Malformed request of size {0} encountered; expected size is {1}", iTotalRead, GatewayPayloadConst.payloadHeaderSize));
                            }

                            GatewayPayload payload = GatewayPayload.ParseHeader(arrHeader, pipeServer, this);

                            // Process the payload
                            try
                            {
                                payload.ProcessPayload(serviceBlockStore, cancellationToken);
                            }
                            catch (Exception ex)
                            {
                                HandleProcessPayloadException(ref payload, ex);
                            }

                            // Send the Data back to the sender
                            payload.SendResponse();
                        }
                    }
                }
                catch (IOException)
                {
                    // Network connection was closed
                }
                catch (ObjectDisposedException)
                {
                    // Network connection was closed
                }
                catch (Exception ex)
                {
                    Console.WriteLine("SFBlockstoreService::ProcessNPClient: Got an exception [{0} - {1}] while handling incoming request.", ex.GetType().ToString(), ex.Message);
                }

                // If we are here, we are not in a position to process the network requests and thus, should close
                // the connection.
                if (pipeServer != null)
                {
                    pipeServer.Close();
                }
            }
        }
        private void ProcessTcpClient(CancellationToken cancellationToken, TcpClient tcpClient)
        {
            if (!cancellationToken.IsCancellationRequested)
            {
                NetworkStream ns        = null;
                byte[]        arrHeader = null;

                try
                {
                    try
                    {
                        ns = tcpClient.GetStream();
                    }
                    catch (InvalidOperationException)
                    {
                        Console.WriteLine("SFBlockstoreService::ProcessTCPClient: Unable to get NetworkStream to read data!");
                        tcpClient.Close();
                        ns = null;
                    }

                    while ((ns != null) && (tcpClient.Connected) && (!cancellationToken.IsCancellationRequested))
                    {
                        // Fetch the header from the stream
                        arrHeader = new byte[GatewayPayloadConst.payloadHeaderSize];
                        int iTotalRead = FetchDataFromTransport(ns, null, arrHeader, GatewayPayloadConst.payloadHeaderSize);
                        if (iTotalRead > 0)
                        {
                            // Did we get a buffer of expected size?
                            if (iTotalRead != GatewayPayloadConst.payloadHeaderSize)
                            {
                                throw new InvalidOperationException(String.Format("SFBlockstoreService::ProcessTCPClient: Malformed request of size {0} encountered; expected size is {1}", iTotalRead, GatewayPayloadConst.payloadHeaderSize));
                            }

                            GatewayPayload payload = GatewayPayload.ParseHeader(arrHeader, ns, this);

                            // Process the payload
                            try
                            {
                                payload.ProcessPayload(serviceBlockStore, cancellationToken);
                            }
                            catch (Exception ex)
                            {
                                HandleProcessPayloadException(ref payload, ex);
                            }

                            // Send the Data back to the sender
                            payload.SendResponse();
                        }
                        else
                        {
                            //Remote socket is terminated.
                            break;
                        }
                    }
                }
                catch (IOException)
                {
                    // Network connection was closed
                }
                catch (ObjectDisposedException)
                {
                    // Network connection was closed
                }
                catch (Exception ex)
                {
                    Console.WriteLine("SFBlockstoreService::ProcessTCPClient: Got an exception [{0} - {1}] while handling incoming request.", ex.GetType().ToString(), ex.Message);
                }

                // If we are here, we are not in a position to process the network requests and thus, should close
                // the connection.
                if (tcpClient != null)
                {
                    tcpClient.Close();
                }
            }
        }