/// <summary>
		/// Initializes a new instance of the <see cref="EpicsGateWayClientChannel"/> class.
		/// </summary>
		/// <param name="gateWay">
		/// The gate way.
		/// </param>
		/// <param name="clientChanId">
		/// The client chan id.
		/// </param>
		/// <param name="channelName">
		/// The channel name.
		/// </param>
		/// <param name="ipAddress">
		/// The ip address.
		/// </param>
		/// <param name="gWClientChanId">
		/// The g w client chan id.
		/// </param>
		internal EpicsGateWayClientChannel(
			EpicsGateWay gateWay, uint clientChanId, string channelName, EndPoint ipAddress, uint gWClientChanId)
		{
			this.GateWay = gateWay;
			this.IpAddress = ipAddress.ToString();
			this.ClientChanId = clientChanId;
			this.GWClientChanId = gWClientChanId;
			this.ChannelName = channelName;

			try
			{
				this.Conn = this.GateWay.TCPConnections[this.IpAddress];
			}
			catch (Exception e)
			{
				Trace.Write("ITCP Connection for Channel was closed before Channel was established (IP: " + this.IpAddress + ")");
				this.Dispose();
				return;
			}

			try
			{
				this.Channel = this.GateWay.ChannelListIocName[channelName];
			}
			catch (Exception e)
			{
				// if it was not yet established, give it a second to find, if not it's to slow and it shall be found next time
				var timeout = new TimeSpan(0, 0, 1);
				var wtch = new Stopwatch();
				wtch.Start();
				while (this.GateWay.ChannelSearchNameWait.ContainsKey(channelName))
				{
					Thread.Sleep(0);

					if (wtch.Elapsed > timeout)
					{
						break;
					}
				}

				wtch.Stop();

				try
				{
					this.Channel = this.GateWay.ChannelListIocName[channelName];
				}
				catch
				{
					Trace.Write("IChannel creation to fast, finding to slow.");
					this.Conn.Send(this.GateWay.ReceiverCodec.channelCreationFailMessage(this.ClientChanId));
					this.Dispose();
					return;
				}
			}

			this.IpAddressEndpoint = ipAddress;
			var calcRights = this.GateWay.Rules.GetAccessRights(this.IpAddressEndpoint, this.Conn.Username, this.ChannelName);
			if (calcRights < this.Channel.AccessRights)
			{
				this.Access = calcRights;
			}
			else
			{
				this.Access = this.Channel.AccessRights;
			}

			gateWay.Rules.RulesChanged += this.Rules_RulesChanged;

			this.Conn.ConnectionStateChanged += this.EpicsGateWayClientChannel_ConnectionStateChanged;

			this.Conn.Send(
				this.GateWay.ReceiverCodec.channelCreatedMessage(
					this.ClientChanId, this.GWClientChanId, this.Access, this.Channel.CreateMessageAnswer));

			this.GateWay.Statistic.OpenClientChannels++;
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="EpicsGateWayClientChannel"/> class.
        /// </summary>
        /// <param name="gateWay">
        /// The gate way.
        /// </param>
        /// <param name="clientChanId">
        /// The client chan id.
        /// </param>
        /// <param name="channelName">
        /// The channel name.
        /// </param>
        /// <param name="ipAddress">
        /// The ip address.
        /// </param>
        /// <param name="gWClientChanId">
        /// The g w client chan id.
        /// </param>
        internal EpicsGateWayClientChannel(
            EpicsGateWay gateWay, uint clientChanId, string channelName, EndPoint ipAddress, uint gWClientChanId)
        {
            this.GateWay        = gateWay;
            this.IpAddress      = ipAddress.ToString();
            this.ClientChanId   = clientChanId;
            this.GWClientChanId = gWClientChanId;
            this.ChannelName    = channelName;

            try
            {
                this.Conn = this.GateWay.TCPConnections[this.IpAddress];
            }
            catch (Exception e)
            {
                Trace.Write("ITCP Connection for Channel was closed before Channel was established (IP: " + this.IpAddress + ")");
                this.Dispose();
                return;
            }

            try
            {
                this.Channel = this.GateWay.ChannelListIocName[channelName];
            }
            catch (Exception e)
            {
                // if it was not yet established, give it a second to find, if not it's to slow and it shall be found next time
                var timeout = new TimeSpan(0, 0, 1);
                var wtch    = new Stopwatch();
                wtch.Start();
                while (this.GateWay.ChannelSearchNameWait.ContainsKey(channelName))
                {
                    Thread.Sleep(0);

                    if (wtch.Elapsed > timeout)
                    {
                        break;
                    }
                }

                wtch.Stop();

                try
                {
                    this.Channel = this.GateWay.ChannelListIocName[channelName];
                }
                catch
                {
                    Trace.Write("IChannel creation to fast, finding to slow.");
                    this.Conn.Send(this.GateWay.ReceiverCodec.channelCreationFailMessage(this.ClientChanId));
                    this.Dispose();
                    return;
                }
            }

            this.IpAddressEndpoint = ipAddress;
            var calcRights = this.GateWay.Rules.GetAccessRights(this.IpAddressEndpoint, this.Conn.Username, this.ChannelName);

            if (calcRights < this.Channel.AccessRights)
            {
                this.Access = calcRights;
            }
            else
            {
                this.Access = this.Channel.AccessRights;
            }

            gateWay.Rules.RulesChanged += this.Rules_RulesChanged;

            this.Conn.ConnectionStateChanged += this.EpicsGateWayClientChannel_ConnectionStateChanged;

            this.Conn.Send(
                this.GateWay.ReceiverCodec.channelCreatedMessage(
                    this.ClientChanId, this.GWClientChanId, this.Access, this.Channel.CreateMessageAnswer));

            this.GateWay.Statistic.OpenClientChannels++;
        }