/// <summary> Logs the given exception and creates an error message to send to the
        /// remote system.
        ///
        /// </summary>
        /// <param name="encoding">The encoding for the error message. If <code>null</code>, uses default encoding
        /// </param>
        public static System.String logAndMakeErrorMessage(System.Exception e, Segment inHeader, Parser p, System.String encoding)
        {
            // create error message ...
            System.String errorMessage = null;
            try
            {
                Message out_Renamed = NuGenDefaultApplication.makeACK(inHeader);
                Terser  t           = new Terser(out_Renamed);

                //copy required data from incoming message ...
                try
                {
                    t.set_Renamed("/MSH-10", MessageIDGenerator.Instance.NewID);
                }
                catch (System.IO.IOException ioe)
                {
                    throw new NuGenHL7Exception("Problem creating error message ID: " + ioe.Message);
                }

                //populate MSA ...
                t.set_Renamed("/MSA-1", "AE");                 //should this come from HL7Exception constructor?
                t.set_Renamed("/MSA-2", Terser.get_Renamed(inHeader, 10, 0, 1, 1));
                System.String excepMessage = e.Message;
                if (excepMessage != null)
                {
                    t.set_Renamed("/MSA-3", excepMessage.Substring(0, (System.Math.Min(80, excepMessage.Length)) - (0)));
                }

                /* Some earlier ACKs don't have ERRs, but I think we'll change this within HAPI
                 * so that there is a single ACK for each version (with an ERR). */
                //see if it's an HL7Exception (so we can get specific information) ...
                if (e.GetType().Equals(typeof(NuGenHL7Exception)))
                {
                    Segment err = (Segment)out_Renamed.get_Renamed("ERR");
                    ((NuGenHL7Exception)e).populate(err);
                }
                else
                {
                    t.set_Renamed("/ERR-1-4-1", "207");
                    t.set_Renamed("/ERR-1-4-2", "Application Internal Error");
                    t.set_Renamed("/ERR-1-4-3", "HL70357");
                }

                if (encoding != null)
                {
                    errorMessage = p.encode(out_Renamed, encoding);
                }
                else
                {
                    errorMessage = p.encode(out_Renamed);
                }
            }
            catch (System.IO.IOException ioe)
            {
                throw new NuGenHL7Exception("IOException creating error response message: " + ioe.Message, NuGenHL7Exception.APPLICATION_INTERNAL_ERROR);
            }
            return(errorMessage);
        }
        /// <summary> Returns the first Application that has been registered, which can process the given
        /// Message (according to its canProcess() method).
        /// </summary>
        private NuGenApplication findApplication(Message message)
        {
            int c = 0;
            NuGenApplication app = null;

            while (app == null && c < this.apps.Count)
            {
                NuGenApplication a = (NuGenApplication)this.apps[c++];
                if (a.canProcess(message))
                {
                    app = a;
                }
            }

            //have to send back an application reject of no apps available to process
            if (app == null)
            {
                app = new NuGenDefaultApplication();
            }
            return(app);
        }
		/// <summary> Returns the first Application that has been registered, which can process the given
		/// Message (according to its canProcess() method).
		/// </summary>
		private NuGenApplication findApplication(Message message)
		{
			int c = 0;
			NuGenApplication app = null;
			while (app == null && c < this.apps.Count)
			{
				NuGenApplication a = (NuGenApplication) this.apps[c++];
				if (a.canProcess(message))
					app = a;
			}
			
			//have to send back an application reject of no apps available to process
			if (app == null)
				app = new NuGenDefaultApplication();
			return app;
		}