private void OnOnlineEvent(object sender, AnnouncementEventArgs e)
 {
     _availableEndpoints.Add(e.EndpointDiscoveryMetadata.Address);
     AvailableEndpointsChanged?.Invoke(this, new EventArgs());
 }
        public void FindAvailableServices()
        {
            try
            {
                IsSearchingForEndPoints = true;

                AvailableEndpointsChanged?.Invoke(this, new EventArgs());

                var discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
                var findResponse    = discoveryClient.Find(new FindCriteria(typeof(IDataServiceServer))
                {
                    Duration = TimeSpan.FromSeconds(2)
                });

                foreach (var endPoints in findResponse.Endpoints)
                {
                    var found = false;

                    foreach (var knownEndpoint in _availableEndpoints)
                    {
                        if (knownEndpoint.Uri == endPoints.Address.Uri)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        lock (_availableEndpoints)
                        {
                            _availableEndpoints.Add(endPoints.Address);
                            AvailableEndpointsChanged?.Invoke(this, new EventArgs());
                        }
                    }
                }

                foreach (var knownEndpoint in _availableEndpoints.ToList())
                {
                    var found = false;
                    foreach (var endPoints in findResponse.Endpoints)
                    {
                        if (knownEndpoint.Uri == endPoints.Address.Uri)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        lock (_availableEndpoints)
                        {
                            _availableEndpoints.Remove(knownEndpoint);
                            AvailableEndpointsChanged?.Invoke(this, new EventArgs());
                        }
                    }
                }
            }
            finally
            {
                IsSearchingForEndPoints = false;

                AvailableEndpointsChanged?.Invoke(this, new EventArgs());
            }
        }
        public void Start()
        {
            lock (_monitorLock)
            {
                if (_running)
                {
                    return;
                }

                _running = true;

                _monitor = Task.Run(() => {
                    while (_running)
                    {
                        foreach (var client in _clients)
                        {
                            if (_closedStates.Contains(client.State))
                            {
                                try
                                {
                                    //Check if we think we are connected by the connection state is broken
                                    if (IsConnected)
                                    {
                                        client.Close();

                                        IsConnected = false;
                                    }

                                    if (_availableEndpoints.Count > 0)
                                    {
                                        if (_connection == null)
                                        {
                                            _connection = _availableEndpoints.FirstOrDefault()?.Uri;
                                        }

                                        var connectionToAttempt = FindEndpoint(_connection);

                                        if (connectionToAttempt is null)
                                        {
                                            //The connection doesn't exist.
                                            _connection = null;
                                        }

                                        if (connectionToAttempt is object)
                                        {
                                            try
                                            {
                                                client.Open(connectionToAttempt);

                                                IsConnected = true;
                                            }
                                            catch (TimeoutException)
                                            {
                                                //Ignore and try again on the next pass
                                                IsConnected = false;
                                            }
                                            catch (CommunicationException)
                                            {
                                                //Looks like the server is dead. Remove from the available list.
                                                _availableEndpoints.Remove(connectionToAttempt);
                                                AvailableEndpointsChanged?.Invoke(this, new EventArgs());
                                                IsConnected = false;
                                            }
                                            catch (Exception)
                                            {
                                                IsConnected = false;
                                            }
                                        }
                                    }
                                }
                                catch
                                {
                                    //Sink
                                }
                            }
                        }

                        _resetEvent.WaitOne(1000);
                    }
                });
            }
        }