/// <summary>
 /// Gets an available server based on Round Robin load balancing
 /// </summary>
 /// <param name="isMaster">True if the server to return must be a master</param>
 /// <returns>Next available server</returns>
 internal protected override ReplicationServer GetServer(bool isMaster)
 {
     for (int i = 0; i < Servers.Count; i++)
     {
         nextServer++;
         if (nextServer == Servers.Count)
         {
             nextServer = 0;
         }
         ReplicationServer s = Servers[nextServer];
         if (!s.IsAvailable)
         {
             continue;
         }
         if (isMaster && !s.IsMaster)
         {
             continue;
         }
         return(s);
     }
     return(null);
 }
 /// <summary>
 /// Handles a failed connection to a server.
 /// </summary>
 /// <param name="server">The failed server</param>
 /// <param name="exception">Exception that caused the failover</param>
 internal protected virtual void HandleFailover(ReplicationServer server, Exception exception)
 {
     HandleFailover(server);
 }
        /// <summary>
        /// Handles a failed connection to a server.
        /// This method can be overrided to implement a custom failover handling
        /// </summary>
        /// <param name="server">The failed server</param>
        internal protected virtual void HandleFailover(ReplicationServer server)
        {
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += delegate(object sender, DoWorkEventArgs e)
            {
                bool isRunning            = false;
                ReplicationServer server1 = e.Argument as ReplicationServer;
#if !RT
                System.Timers.Timer timer = new System.Timers.Timer(RetryTime * 1000.0);

                System.Timers.ElapsedEventHandler elapsedEvent = delegate(object sender1, System.Timers.ElapsedEventArgs e1)
                {
                    if (isRunning)
                    {
                        return;
                    }
                    try
                    {
                        isRunning = true;
                        using (MyCatConnection connectionFailed = new MyCatConnection(server.ConnectionString))
                        {
                            connectionFailed.Open();
                            server1.IsAvailable = true;
                            timer.Stop();
                        }
                    }
                    catch
                    {
                        MyCatTrace.LogWarning(0,
                                              string.Format(Properties.Resources.Replication_ConnectionAttemptFailed, server1.Name));
                    }
                    finally
                    {
                        isRunning = false;
                    }
                };
                timer.Elapsed += elapsedEvent;
                timer.Start();
                elapsedEvent(sender, null);
#else
                Windows.UI.Xaml.DispatcherTimer timer = new Windows.UI.Xaml.DispatcherTimer();
                TimeSpan ts = new TimeSpan(RetryTime * 1000);
                System.EventHandler <object> elapsedEvent = (TickSender, TickEventArgs) =>
                {
                    if (isRunning)
                    {
                        return;
                    }
                    try
                    {
                        isRunning = true;
                        using (MyCatConnection connectionFailed = new MyCatConnection(server.ConnectionString))
                        {
                            connectionFailed.Open();
                            server1.IsAvailable = true;
                            timer.Stop();
                        }
                    }
                    catch
                    {
                        MyCatTrace.LogWarning(0,
                                              string.Format(Properties.Resources.Replication_ConnectionAttemptFailed, server1.Name));
                    }
                    finally
                    {
                        isRunning = false;
                    }
                };
                timer.Tick += elapsedEvent;
                elapsedEvent(sender, null);
                timer.Start();
#endif
            };

            worker.RunWorkerAsync(server);
        }