agsXMPP.XmppClientConnection.PickSRVRecord C# (CSharp) Method

PickSRVRecord() private method

Picks one of the SRV records. priority and weight are evaluated by the following algorithm.
private PickSRVRecord ( ) : agsXMPP.Net.Dns.SRVRecord
return agsXMPP.Net.Dns.SRVRecord
        private SRVRecord PickSRVRecord()
        {
            SRVRecord ret = null;

            // total weight of all servers with the same priority
            int totalWeight = 0;
            
            // ArrayList for the servers with the lowest priority
            ArrayList lowServers = new ArrayList();
            // check we have a response
            if (_SRVRecords != null && _SRVRecords.Length > 0)
            {
                // Find server(s) with the highest priority (could be multiple)
                foreach (SRVRecord srv in _SRVRecords)
                {
                    if (ret == null)
                    {
                        ret = srv;
                        lowServers.Add(ret);
                        totalWeight = ret.Weight;
                    }
                    else
                    {
                        if (srv.Priority == ret.Priority)
                        {
                            lowServers.Add(srv);
                            totalWeight += srv.Weight;
                        }
                        else if (srv.Priority < ret.Priority)
                        {
                            // found a servr with a lower priority
                            // clear the lowServers Array and start with this server
                            lowServers.Clear();
                            lowServers.Add(ret);
                            ret = srv;
                            totalWeight = ret.Weight;
                        }
                        else if (srv.Priority > ret.Priority)
                        {
                            // exit the loop, because servers are already sorted by priority
                            break;
                        }
                    }
                }
            }

            // if we have multiple lowServers then we have to pick a random one
            // BUT we have too involve the weight which can be used for "Load Balancing" here
            if (lowServers.Count > 1)
            {
                if (totalWeight > 0)
                {
                    // Create a random value between 1 - total Weight
                    int rnd = new Random().Next(1, totalWeight);
                    int i = 0;
                    foreach (SRVRecord sr in lowServers)
                    {
                        if (rnd > i && rnd <= (i + sr.Weight))
                        {
                            ret = sr;
                            break;
                        }
                        else
                        {
                            i += sr.Weight;
                        }
                    }
                }
                else
                {
                    // Servers have no weight, they are all equal, pick a random server
                    int rnd = new Random().Next(lowServers.Count);                    
                    ret = (SRVRecord) lowServers[rnd];                    
                }
            }

            return ret;
        }