/// <summary>
        /// Sends the request and waits for a response
        /// </summary>
        /// <param name="request">The request to be processed by the server and for which we want a response.</param>
        /// <returns></returns>
        public virtual HttpResponse GetResponse(
            HttpRequest request,
            EventHandler <HttpMessageProgressEventArgs> onSendProgress,
            EventHandler <HttpMessageProgressEventArgs> onRecvProgress,
            object stateObject)
        {
            try
            {
                // send the request
                this.SendRequest(request, onSendProgress, stateObject);

                HttpResponse response         = null;
                bool         recievedContinue = false;
                do
                {
                    recievedContinue = false;

                    lock (_messageReader)
                    {
                        // receive a response
                        HttpMessage message = _messageReader.Read(_socket, null, onRecvProgress, stateObject);
                        if (message.Type == HttpMessageTypes.HttpResponse)
                        {
                            response = new HttpResponse(message);
                        }
                    }

                    if (response != null)
                    {
                        Debug.WriteLineIf(_verbose, "Logging response...", MY_TRACE_CATEGORY);
                        Debug.WriteIf(_verbose, response.ToString(false));

                        if (response.Status.Code == HttpStatusCode.Continue)
                        {
                            recievedContinue = true;
                            response         = null;
                        }
                    }
                }while (recievedContinue);

                return(response);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
                this.Close();
            }
            return(null);
        }
        /// <summary>
        /// Tries to receive data from the network on a background thread
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void OnReadFromSocket(object sender, BackgroundThreadStartEventArgs threadStartArgs)
        {
            try
            {
                // create a new message reader
                _messageReader = new HttpMessageReader();

                while (true)
                {
                    try
                    {
                        // read a single message
                        HttpMessage message = null;

                        // lock the reader
                        lock (_messageReader)
                        {
                            // read a message
                            message = _messageReader.Read(_socket, _stopEvent);
                        }

                        // what type of message is this ?
                        switch (message.Type)
                        {
                        /*
                         * process the request
                         * */
                        case HttpMessageTypes.HttpRequest:
                        {
                            // process the request by dispatching it and then responding if need be
                            this.OnRequestReceived(this, new HttpRequestCancelEventArgs(new HttpRequest(message), false));
                            break;
                        }

                        /*
                         * process the response
                         * */
                        case HttpMessageTypes.HttpResponse:
                        {
                            this.OnResponseReceived(this, new HttpResponseEventArgs(new HttpResponse(message)));
                            break;
                        }

                        /*
                         * an unknown message type
                         * */
                        default:
                        {
                            // hopefully this will never happen!
                            Debug.WriteLine(string.Format("A message of unknown type was received from '{0}'...\n{1}", message));
                            break;
                        }
                        }
                        ;
                    }
                    catch (HttpMessageReaderAbortedException)
                    {
                        // the reader was aborted
                    }
                    catch (HttpConnectionClosedByPeerException)
                    {
                        /*
                         * the remote host has closed the connection
                         * */
                        this.Close();
                        return;
                    }

                    // see if we should stop receiving
                    if (_stopEvent != null)
                    {
                        if (_stopEvent.WaitOne(1, false))
                        {
                            /*
                             * we have recieved a signal that we should stop receiving
                             * and disconnect the current connection
                             * */
                            if (_disconnectOnStop)
                            {
                                this.Close();
                            }
                            return;
                        }
                    }
                }
            }
            catch (ThreadAbortException)
            {
                /*
                 * the thread is aborting
                 * */
                if (_disconnectOnStop)
                {
                    this.Close();
                }
            }
            catch (ObjectDisposedException)
            {
                /*
                 * the connection has closed the socket
                 * */
                this.Close();
            }
            catch (SocketException ex)
            {
                // if the connection is reset, or a blocking call was cancelled with a call to cancelblockingcall
                if (ex.ErrorCode == (int)SocketErrors.WSAECONNRESET ||
                    ex.ErrorCode == (int)SocketErrors.WSAEINTR)
                {
                    this.Close();
                    return;
                }

                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
            }
            catch (Exception ex)
            {
                // notify that this connection has encountered an exception
                this.OnException(this, new ExceptionEventArgs(ex));
            }
        }
		/// <summary>
		/// Tries to receive data from the network on a background thread
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected virtual void OnReadFromSocket(object sender, BackgroundThreadStartEventArgs threadStartArgs) 
		{
			try 
			{	               
				// create a new message reader
				_messageReader = new HttpMessageReader();
				
				while(true) 
				{
					try 
					{			
						// read a single message
						HttpMessage message = null;
							
						// lock the reader
						lock(_messageReader)
						{
							// read a message
							message = _messageReader.Read(_socket, _stopEvent);
						}

						// what type of message is this ?						
						switch(message.Type)
						{
							/*
							 * process the request
							 * */
							case HttpMessageTypes.HttpRequest:
							{	
								// process the request by dispatching it and then responding if need be
                                this.OnRequestReceived(this, new HttpRequestCancelEventArgs(new HttpRequest(message), false));								
								break;
							}
							/*
							 * process the response
							 * */
							case HttpMessageTypes.HttpResponse:
							{
								this.OnResponseReceived(this, new HttpResponseEventArgs(new HttpResponse(message)));
								break;
							}
								/*
								 * an unknown message type
								 * */
							default:
							{	
								// hopefully this will never happen!
								Debug.WriteLine(string.Format("A message of unknown type was received from '{0}'...\n{1}", message));	
								break;
							}							
						};
					}
					catch(HttpMessageReaderAbortedException)
					{
						// the reader was aborted
					}
					catch(HttpConnectionClosedByPeerException) 
					{					
						/*
						* the remote host has closed the connection
						* */
						this.Close();
						return;
					}
					
					// see if we should stop receiving
					if (_stopEvent != null)
						if (_stopEvent.WaitOne(1, false)) 
						{
							/*
							* we have recieved a signal that we should stop receiving
							* and disconnect the current connection
							* */
							if (_disconnectOnStop)
								this.Close();
							return;
						}							
				}
												
			}
			catch(ThreadAbortException) 
			{
				/*
				 * the thread is aborting
				 * */
				if (_disconnectOnStop)
					this.Close();
			}
			catch(ObjectDisposedException) 
			{
				/*
				 * the connection has closed the socket
				 * */				
				this.Close();
			}
			catch(SocketException ex) 
			{
				// if the connection is reset, or a blocking call was cancelled with a call to cancelblockingcall
				if (ex.ErrorCode == (int)SocketErrors.WSAECONNRESET ||
					ex.ErrorCode == (int)SocketErrors.WSAEINTR) 
				{
					this.Close();
					return;
				}

				// notify that this connection has encountered an exception
				this.OnException(this, new ExceptionEventArgs(ex));
			}
			catch(Exception ex) 
			{
				// notify that this connection has encountered an exception
				this.OnException(this, new ExceptionEventArgs(ex));
			}
		}