private void HandleEvent(GatewayPayload evnt, TaskCompletionSource <bool> readySignal)
        {
            switch (evnt.Operation)
            {
            case GatewayOperation.Dispatch:
                HandleDispatchEvent(evnt, readySignal);
                break;

            case GatewayOperation.InvalidSession:
                if ((bool)evnt.Data != true)     // Is resumable
                {
                    SetSession(null);
                }
                readySignal.TrySetResult(false);
                GatewayInvalidSession?.Invoke((bool)evnt.Data);
                break;

            case GatewayOperation.Heartbeat:
                SendHeartbeatAck();
                GatewayHeartbeat?.Invoke();
                break;

            case GatewayOperation.HeartbeatAck: GatewayHeartbeatAck?.Invoke(); break;

            case GatewayOperation.Hello: GatewayHello?.Invoke(evnt.Data as HelloEvent); break;

            case GatewayOperation.Reconnect: GatewayReconnect?.Invoke(); break;
            }
        }
        //// Listening multicast to get reports
        private void StartListener()
        {
            //// Launching multicast listener on gateway multicast IP
            PackageHost.WriteInfo("Starting to listen multicast on gateway multicast IP");
            UdpClient  client           = new UdpClient();
            IPAddress  multicastaddress = IPAddress.Parse(PackageHost.GetSettingValue <string>("GatewayMulticastIP"));
            IPEndPoint localEp          = new IPEndPoint(IPAddress.Any, PackageHost.GetSettingValue <int>("GatewayMulticastPort"));

            client.ExclusiveAddressUse = false;
            client.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            client.ExclusiveAddressUse = false;
            client.Client.Bind(localEp);
            try
            {
                //// Join multicast
                client.JoinMulticastGroup(multicastaddress);
                PackageHost.WriteInfo("Listening start...");
                while (PackageHost.IsRunning)
                {
                    try
                    {
                        //// We convert byte to string
                        byte[] bytes    = client.Receive(ref localEp);
                        string response = System.Text.Encoding.ASCII.GetString(bytes, 0, bytes.Length);

                        //// We deserialize to Response class
                        Response data = JsonConvert.DeserializeObject <Response>(response);

                        //// If command equal heartbeat
                        if (data.Cmd == "heartbeat")
                        {
                            //// We print response if heartbeat logs is true
                            if (PackageHost.GetSettingValue <bool>("HeartbeatLog"))
                            {
                                PackageHost.WriteInfo("{0}", response);
                            }

                            //// If heartbeat model is gateway, we save the data part to Gateway class
                            if (data.Model == "gateway")
                            {
                                //// We save the token
                                gateway.Token = data.Token;

                                //// We deserialize data part
                                GatewayHeartbeat gateway_data = JsonConvert.DeserializeObject <GatewayHeartbeat>(data.Data);

                                //// If we didn't set gateway's sid
                                if (gateway.Sid == null)
                                {
                                    //// If data ip equal to gateway ip
                                    if (gateway_data.IP == PackageHost.GetSettingValue <string>("GatewayIP"))
                                    {
                                        gateway.Sid = data.Sid;
                                    }
                                }

                                //// We push the gateway SO
                                PackageHost.PushStateObject <Gateway>("Gateway", gateway);
                            }
                        }
                        //// If command equal report
                        else if (data.Cmd == "report")
                        {
                            //// We print response if report logs is true
                            if (PackageHost.GetSettingValue <bool>("ReportLog"))
                            {
                                PackageHost.WriteInfo("{0}", response);
                            }

                            //// If model equal gateway
                            if ((data.Model == "gateway") && (data.Sid == gateway.Sid))
                            {
                                //// We deserialize report data as gateway class
                                GatewayReport datagate = JsonConvert.DeserializeObject <GatewayReport>(data.Data);
                                gateway.Report = datagate;

                                //// We push the gateway SO
                                PackageHost.PushStateObject <Gateway>("Gateway", gateway);
                            }
                            else
                            {
                                //// If equipement sid is in the equipements list
                                if (equipements.Contains(data.Sid))
                                {
                                    this.SaveEquipement(data.Sid);
                                }
                            }
                        }
                        Thread.Yield();
                    }
                    catch (Exception ex)
                    {
                        PackageHost.WriteError("Error with receive message : {0}", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                PackageHost.WriteError("Error joigning multicast : {0}", ex);
                return;
            }
        }