public async void AwaitMulticastRequest()
        {
            try
            {
                //Listen on port 4568 since its Unicast and cant be bound to port 4567 (in use for multicast)
                using (var client = new UdpClient(MulticastEndPoint.Port, AddressFamily.InterNetwork))
                {
                    client.Ttl = 2;
                    client.JoinMulticastGroup(MulticastEndPoint.Address);
                    client.Client.ReceiveBufferSize = 18;
                    var ipEndPoint = new IPEndPoint(IPAddress.Loopback, MulticastEndPoint.Port);
                    while (true)
                    {
                        Debug.WriteLine("Awaiting multicast request");
                        var result = await client.ReceiveAsync();

                        var stateProtocol = StateProtocol.FromBytes(result.Buffer);

                        //Check if reply is an actual discovery reply
                        if (stateProtocol.ProtocolType == ProtocolRequest.DeviceDiscovery)
                        {
                            if (ipEndPoint.Address.Equals(IPAddress.Parse("127.0.0.1")))
                            {
                                return;
                            }

                            var robeatsDevice = new RobeatsDevice
                            {
                                Id            = stateProtocol.DeviceId,
                                Name          = stateProtocol.DeviceName,
                                EndPoint      = new IPEndPoint(ipEndPoint.Address, 4568),
                                StateProtocol = stateProtocol
                            };

                            OnDeviceDetect(new DeviceDiscoveryEventArgs {
                                RobeatsDevice = robeatsDevice
                            });
                        }
                        else
                        {
                            Debug.WriteLine("Not a reply. ignoring");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }
 public void SendDiscoveryReply(RobeatsDevice device)
 {
     //Check if reply is an actual discovery reply
     if (device.StateProtocol.ProtocolType == ProtocolRequest.DeviceDiscovery)
     {
         var stateProtocolReply =
             new StateProtocol(ProtocolRequest.DeviceDiscoveryReply, device.Name, device.Id);
         var bytesReply  = stateProtocolReply.ToBytes();
         var clientReply = new UdpClient(AddressFamily.InterNetwork)
         {
             Ttl = 2
         };
         Debug.WriteLine($"Sending DR: {device.EndPoint}");
         clientReply.Send(bytesReply, bytesReply.Length, (IPEndPoint)device.EndPoint);
         clientReply.Close();
     }
     else
     {
         Debug.WriteLine("Not a discoveryRequest. ignoring");
     }
 }
        /// <summary>
        /// Send a request to a specific <see cref="IPEndPoint"/>
        /// </summary>
        /// <param name="requestType"></param>
        /// <param name="robeatsDevice"></param>
        public void SendRequest(ProtocolRequest requestType, RobeatsDevice robeatsDevice)
        {
            if (!Visible)
            {
                return;
            }
            UdpHelper udpHelper;

            if (requestType == ProtocolRequest.DeviceDiscovery)
            {
                udpHelper = new UdpHelper(MulticastEndPoint);
            }
            else
            {
                var ipEndPoint = new IPEndPoint(((IPEndPoint)robeatsDevice.EndPoint).Address, 4568);
                udpHelper = new UdpHelper(ipEndPoint);
            }

            var stateProtocol = new StateProtocol(requestType, robeatsDevice.Name, robeatsDevice.Id);

            Debug.WriteLine("Sending request of type: " + requestType);
            udpHelper.Send(stateProtocol.ToBytes());
            udpHelper.Close();
        }
        public async void AwaitDiscoveryReply()
        {
            try
            {
                using (var client = new UdpClient(4568, AddressFamily.InterNetwork))
                {
                    while (true)
                    {
                        Debug.WriteLine("Awaiting DR");

                        var result = await client.ReceiveAsync();

                        Debug.WriteLine("Received DR");
                        var stateProtocol = StateProtocol.FromBytes(result.Buffer);

                        if (stateProtocol.ProtocolType == ProtocolRequest.DeviceDiscoveryReply)
                        {
                            var robeatsDevice = new RobeatsDevice
                            {
                                Id   = stateProtocol.DeviceId,
                                Name = stateProtocol.DeviceName,
                                /*EndPoint = client.Client.RemoteEndPoint,*/
                                StateProtocol = stateProtocol
                            };
                            OnDiscoveryReply(new DeviceDiscoveryEventArgs {
                                RobeatsDevice = robeatsDevice
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
        }