/// <summary>
 /// Initializes a new instance of the HttpConnection class (Clients use this contructor)
 /// </summary>
 public HttpConnection(bool verbose)
 {
     _verbose       = verbose;
     _id            = Guid.NewGuid();
     _dispatcher    = new HttpRequestDispatcher(false);
     _messageWriter = new HttpMessageWriter();
     _messageReader = new HttpMessageReader();
 }
        /// <summary>
        /// Initializes a new instance of the HttpConnection class (Servers use this constructor)
        /// </summary>
        /// <param name="socket">The socket to be used for the lifetime of the connection</param>
        public HttpConnection(Socket socket, bool verbose)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket", "A connection cannot be created using a null socket.");
            }

            _verbose       = verbose;
            _socket        = socket;
            _id            = Guid.NewGuid();
            _dispatcher    = new HttpRequestDispatcher(false);
            _messageWriter = new HttpMessageWriter();
            _messageReader = new HttpMessageReader();
        }
        public HttpConnection(IPEndPoint ep, bool beginReceiving, bool verbose, int sendTimeout, int recvTimeout)
        {
            if (ep == null)
            {
                throw new ArgumentNullException("ep");
            }

            _verbose       = verbose;
            _id            = Guid.NewGuid();
            _dispatcher    = new HttpRequestDispatcher(false);
            _messageWriter = new HttpMessageWriter();
            _messageReader = new HttpMessageReader();

            this.Open(ep, beginReceiving, sendTimeout, recvTimeout);
        }
        /// <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>
		/// Initializes a new instance of the HttpConnection class (Servers use this constructor)
		/// </summary>
		/// <param name="socket">The socket to be used for the lifetime of the connection</param>
		public HttpConnection(Socket socket, bool verbose) 
		{
			if (socket == null)
				throw new ArgumentNullException("socket", "A connection cannot be created using a null socket.");

			_verbose = verbose;
			_socket = socket;
			_id = Guid.NewGuid();				
			_dispatcher = new HttpRequestDispatcher(false);
			_messageWriter = new HttpMessageWriter();	
			_messageReader = new HttpMessageReader();
		}
		/// <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));
			}
		}
		public HttpConnection(IPEndPoint ep, bool beginReceiving, bool verbose, int sendTimeout, int recvTimeout)
		{
			if (ep == null)
				throw new ArgumentNullException("ep");

			_verbose = verbose;			
			_id = Guid.NewGuid();				
			_dispatcher = new HttpRequestDispatcher(false);
			_messageWriter = new HttpMessageWriter();	
			_messageReader = new HttpMessageReader();

			this.Open(ep, beginReceiving, sendTimeout, recvTimeout);
		}
		/// <summary>
		/// Initializes a new instance of the HttpConnection class (Clients use this contructor)
		/// </summary>
		public HttpConnection(bool verbose) 
		{
			_verbose = verbose;
			_id = Guid.NewGuid();	
			_dispatcher = new HttpRequestDispatcher(false);
			_messageWriter = new HttpMessageWriter();
			_messageReader = new HttpMessageReader();
		}