/// <summary>
        /// Sends a message synchronously. Does not process exceptions!
        /// </summary>
        /// <param name="content">The content being sent to the remote host.</param>
        /// <param name="timeout">The sending must be completed before this moment.</param>
        public void LowLevel_SendSync(Stream content, int timeout)
        {
            // apply the connection-level security session
            if (this.ConnectionLevelSecurity != null)
            {
                GenuineChunkedStream outputStream = new GenuineChunkedStream(true);
                this.ConnectionLevelSecurity.Encrypt(content, outputStream);
                content = outputStream;
            }

            for ( ; ;)
            {
                if (!this.IsValid)
                {
                    throw GenuineExceptions.Get_Processing_TransportConnectionFailed();
                }
                if (!GenuineUtility.WaitOne(_namedEventRemoteReadCompleted.ManualResetEvent, GenuineUtility.GetMillisecondsLeft(timeout)))
                {
                    throw GenuineExceptions.Get_Send_Timeout();
                }
                if (this._closed != 0)
                {
                    throw GenuineExceptions.Get_Receive_ConnectionClosed();
                }
                this._namedEventRemoteReadCompleted.ManualResetEvent.Reset();

                // read the next portion
                int bytesRead = content.Read(this._sendBuffer, 0, this._sendSpaceSize);

                // LOG:
                BinaryLogWriter binaryLogWriter = this.ITransportContext.BinaryLogWriter;
                if (binaryLogWriter != null && binaryLogWriter[LogCategory.LowLevelTransport] > 0)
                {
                    binaryLogWriter.WriteTransportContentEvent(LogCategory.LowLevelTransport, "GenuineSaredMemoryConnection.LowLevel_SendSync",
                                                               LogMessageType.LowLevelTransport_AsyncSendingInitiating, null, null, this.Remote,
                                                               binaryLogWriter[LogCategory.LowLevelTransport] > 1 ? new MemoryStream(this._sendBuffer, 0, bytesRead) : null,
                                                               GenuineUtility.CurrentThreadId, Thread.CurrentThread.Name,
                                                               this.DbgConnectionId, bytesRead,
                                                               null, null, null,
                                                               "Content sending.");
                }

                Marshal.Copy(this._sendBuffer, 0,
                             (IntPtr)(this._pointer.ToInt32() + this._sendOffset + SHARE_CONTENT_OFFSET),
                             bytesRead);
                this._MessageToSendTotalSize  = bytesRead;
                this._MessageToSendFinishFlag = bytesRead < this._sendSpaceSize ? 1 : 0;

                this._namedEventWriteCompleted.ManualResetEvent.Set();

                if (bytesRead < this._sendSpaceSize)
                {
                    this.UpdateLastTimeAMessageWasSent();
                    return;
                }
            }
        }
        /// <summary>
        /// Completes the HTTP request.
        /// </summary>
        /// <param name="ar">The result of the HTTP request.</param>
        private void OnRequestCompleted(IAsyncResult ar)
        {
            HttpWebResponse httpWebResponse = null;
            Stream          inputStream     = null;
            ConnectionInfo  connectionInfo  = null;

            try
            {
                connectionInfo = (ConnectionInfo)ar.AsyncState;
                HttpWebRequest httpWebRequest = connectionInfo.HttpWebRequest;
#if (FRM20)
                // timeout has been already set
                try
                {
                    httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                }
                catch (WebException ex)
                {
                    if (ex.Status == WebExceptionStatus.Timeout)
                    {
                        return;
                    }
                }
#else
                httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar);
#endif
                this.Remote.Renew(this._hostRenewingSpan, false);

                // process the content
                inputStream = httpWebResponse.GetResponseStream();

#if DEBUG
//				if (this.ITransportContext.IEventLogger.AcceptBinaryData)
//				{
//					byte[] content = new byte[(int) httpWebResponse.ContentLength];
//					GenuineUtility.ReadDataFromStream(inputStream, content, 0, content.Length);
//
//					this.ITransportContext.IEventLogger.Log(LogMessageCategory.Traffic, null, "HttpInvocationConnection.OnRequestCompleted",
//						content, "The content of the response received by the HttpInvocationConnection. Size: {0}.", content.Length);
//					inputStream = new MemoryStream(content, false);
//				}
#endif

                BinaryReader   binaryReader = new BinaryReader(inputStream);
                string         serverUri;
                int            sequenceNo;
                HttpPacketType httpPacketType;
                int            remoteHostUniqueIdentifier;
                HttpMessageCoder.ReadResponseHeader(binaryReader, out serverUri, out sequenceNo, out httpPacketType, out remoteHostUniqueIdentifier);

#if DEBUG
//				this.ITransportContext.IEventLogger.Log(LogMessageCategory.TransportLayer, null, "HttpInvocationConnection.OnRequestCompleted",
//					null, "The invocation request returned. Server uri: {0}. Sequence no: {1}. Packet type: {2}. Content-encoding: {3}. Content-length: {4}. Protocol version: {5}. Response uri: \"{6}\". Server: \"{7}\". Status code: {8}. Status description: \"{9}\".",
//					serverUri, sequenceNo, Enum.Format(typeof(HttpPacketType), httpPacketType, "g"),
//					httpWebResponse.ContentEncoding, httpWebResponse.ContentLength,
//					httpWebResponse.ProtocolVersion, httpWebResponse.ResponseUri,
//					httpWebResponse.Server, httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
#endif

                // if the remote host has asked to terminate a connection
                if (httpPacketType == HttpPacketType.ClosedManually || httpPacketType == HttpPacketType.Desynchronization || httpPacketType == HttpPacketType.SenderError)
                {
                    throw GenuineExceptions.Get_Receive_ConnectionClosed();
                }

                // skip the first byte
                if (binaryReader.ReadByte() != 0)
                {
                    if (!connectionInfo.Message.IsOneWay)
                    {
//						this.ITransportContext.IEventLogger.Log(LogMessageCategory.FatalError, GenuineExceptions.Get_Processing_LogicError("The invocation response doesn't contain any messages."), "HttpInvocationConnection.OnRequestCompleted",
//							null, "The HTTP response doesn't contain content. The response to the request was not received.");
                    }
                }
                else
                {
                    // fetch and process the response messages
                    using (BufferKeeper bufferKeeper = new BufferKeeper(0))
                    {
                        using (LabelledStream labelledStream = new LabelledStream(this.ITransportContext, inputStream, bufferKeeper.Buffer))
                        {
                            GenuineChunkedStream receivedRequest = new GenuineChunkedStream(true);
                            GenuineUtility.CopyStreamToStream(labelledStream, receivedRequest);
                            this.ITransportContext.IIncomingStreamHandler.HandleMessage(receivedRequest, this.Remote, GenuineConnectionType.Invocation, string.Empty, -1, true, null, null, null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
//				this.ITransportContext.IEventLogger.Log(LogMessageCategory.Error, ex, "HttpInvocationConnection.OnRequestCompleted",
//					null, "Exception occurred during receiving a response to an invocation request.");

                // dispatch the exception to the caller context
                if (connectionInfo != null)
                {
                    this.ITransportContext.IIncomingStreamHandler.DispatchException(connectionInfo.Message, ex);
                }
            }
            finally
            {
                if (inputStream != null)
                {
                    inputStream.Close();
                }
                if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }

                // release the connection
                if (connectionInfo != null)
                {
                    this.ReleaseConnectionEntry(connectionInfo.Index);
                }
            }
        }