/// <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 
					{			
//						if (!_isServerSideConnection)
//							Debug.WriteLine(string.Format("User-Agent connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);
//						else
//							Debug.WriteLine(string.Format("Server-Side connection '{0}' trying to read next incoming message from '{1}'", _id.ToString(), this.RemoteAddress.ToString()), MY_TRACE_CATEGORY);

						// 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:
							{	
								// create a request event args
								HttpRequestCancelEventArgs e = new HttpRequestCancelEventArgs(new HttpRequest(message), false);					
								
								// process the request by dispatching it and then responding if need be
								this.OnRequestReceived(this, e);
								
								//								// next check the request to see if the connection should be closed after the response was sent
								//								if (HttpUtils.Contains(e.Request.Connection, HttpConnections.Close))
								//								{
								//									// yup, they wanted us to close the connection automatically so lets do that now
								//									this.Close();
								//									return;
								//								}
								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));
			}
//			finally
//			{
//				Debug.WriteLineIf(!_isServerSideConnection, string.Format("*** exiting receiving thread loop for connection '{0}'", _id.ToString()), MY_TRACE_CATEGORY);
//			}
		}