/// <summary>
        /// Groups the messages that need to be sent. Each message added to
        /// the packet is marked as "Sending"
        /// </summary>
        /// <param name="packet">The resulting packet</param>
        /// <param name="msgIds">The identifiers of the messages in the packet. Only keys
        /// are important</param>
        /// <returns>The number of messages in the packet</returns>
        private int GetMessagesPacket(out StringBuilder packet, out Hashtable msgIds)
        {
            packet = null;
            msgIds = null;
            int msgCount    = 0;
            int currentSize = _packetOverhead;

            lock (_msgStatus)
            {
                AppSettingsReader appSettings = new System.Configuration.AppSettingsReader();
                double            nDifHour    = 0;
                try
                {
                    nDifHour = (double)appSettings.GetValue("HOUR_DIFFERENCE", typeof(double));
                }
                catch
                {
                    nDifHour = 0;
                }

                bool        enoughData = false;
                IEnumerator en         = _msgStatus.Values.GetEnumerator();
                while (en.MoveNext() && !enoughData)
                {
                    MessageData msg = (MessageData)en.Current;
                    // Before atempting to include the message, see if it has
                    // already expired
                    FailMessageIfExpired(msg, DateTime.Now.AddHours(nDifHour));
                    // Send only messages not successfuly sent nor failed
                    // and still having pending retries
                    if (msg.Status != MessageStatus.Sent &&
                        msg.Status != MessageStatus.Failed &&
                        msg.Status != MessageStatus.SendFailed &&
                        msg.Status != MessageStatus.NoMedia &&
                        msg.PendingRetries > 0)
                    {
                        if (packet == null)
                        {
                            packet = new StringBuilder(256);
                            packet.AppendFormat(null, "<{0} {1}=\"{2}\" {3}=\"{4}\">",
                                                Tags.Packet, Tags.PacketSrcAttr, _originId,
                                                Tags.PacketDateAttr, Dtx.DtxToString(DateTime.Now.AddHours(nDifHour)));
                            msgIds = new Hashtable(_msgStatus.Count);
                        }
                        // Add the message only if it doesn't make the packet
                        // exceed the maximum size
                        if (currentSize + msg.XmlData.Length < _maxPacketSize)
                        {
                            packet.Append(msg.XmlData);
                            msgIds.Add(msg.MessageId, msg.MessageId);
                            msg.Status = MessageStatus.Sending;
                            msgCount++;
                            currentSize += msg.XmlData.Length;
                        }
                        else
                        {
                            enoughData = true;
                        }
                    }
                }
                if (packet != null)
                {
                    packet.AppendFormat(null, "</{0}>", Tags.Packet);
                }
            }
            return(msgCount);
        }
        private void OnInQueueReceiveCompleted(FecsBecsHeader header, string body)
        {
            if (_channelMgr != null)
            {
                string   uri     = header.ID;
                IChannel channel = _channelMgr.GetChannel(uri);
                if (channel == null)
                {
                    channel = _channelMgr.OpenChannel(uri, ChannelType.BecsQueue);
                    if (channel != null)
                    {
                        MessageProcessorManager.OnNewConnection(channel);
                    }
                }
                if (channel != null)
                {
                    BecsChannel   bc     = (BecsChannel)channel;
                    StringBuilder packet = new StringBuilder(256);
                    packet.AppendFormat(null, "<{0} {1}=\"{2}\" {3}=\"{4}\">{5}</{0}>",
                                        Tags.Packet, Tags.PacketSrcAttr, header.PacketInfo.SourceId,
                                        Tags.PacketDateAttr, Dtx.DtxToString(header.PacketInfo.Dtx), body);
                    string  xml         = packet.ToString();
                    ILogger localLogger = null;
                    try
                    {
                        Database   d        = null;
                        CmpUnitsDB cmpUnits = new CmpUnitsDB();
                        //d = DatabaseFactory.GetDatabase();
                        localLogger = DatabaseFactory.Logger;

                        if (localLogger != null)
                        {
                            localLogger.AddLog("[OnInQueueReceiveCompleted]: Updating ID: " + header.PacketInfo.SourceId + " IP :" + header.IP, LoggerSeverities.Debug);
                        }
                        if (Convert.ToInt32(header.PacketInfo.SourceId) != -1)
                        {
                            if (cmpUnits.UpdateIP(Convert.ToInt32(header.PacketInfo.SourceId), header.IP) != 1)
                            {
                                if (localLogger != null)
                                {
                                    localLogger.AddLog("[OnInQueueReceiveCompleted]: Error Updating IP", LoggerSeverities.Debug);
                                }
                            }
                            else
                            {
                                if (localLogger != null)
                                {
                                    localLogger.AddLog("[OnInQueueReceiveCompleted]: Update OK", LoggerSeverities.Debug);
                                }
                            }
                        }
                    }
                    catch
                    {
                        // Do not do nothing??
                        // Si tenemos un error en realizar un update de la ip
                        // no hacemos nada
                        if (localLogger != null)
                        {
                            localLogger.AddLog("[OnInQueueReceiveCompleted]: Error Updating IP", LoggerSeverities.Debug);
                        }
                    }
                    Interlocked.Increment(ref _packetId);
                    OPSTelegrama opsTel = OPSTelegramaFactory.CreateOPSTelegrama(_packetId, xml);
                    bc.ReceiveMessage(opsTel);
                }
            }
        }