def connect_to_realm(realm)
      possible_peers = []
      @res.query("_diameter._tcp.#{realm}", "SRV").each_answer do |a|
        possible_peers << {name: a.target.to_s, port: a.port, priority: a.priority, weight: a.weight}
      end

      # Prefer the lowest priority and the highest weight
      possible_peers.sort!{ |a, b| (a[:priority] <=> b[:priority]) || (b[:weight] <=> a[:weight])}
      Diameter.logger.debug("Sorted list of peers for realm #{realm} is #{possible_peers.inspect}")

      primary = possible_peers[0]

      url = "aaa://#{primary[:name]}:#{primary[:port]}"
      Diameter.logger.info("Primary peer for realm #{realm} is #{primary[:name]}, (#{url})")
      connect_to_peer(url, primary[:name], realm)
    end
def connect_to_realm(realm)
      possible_peers = []
      @res.query("_diameter._tcp.#{realm}", "SRV").each_answer do |a|
        possible_peers << {name: a.target.to_s, port: a.port, priority: a.priority, weight: a.weight}
      end

      # Prefer the lowest priority and the highest weight
      possible_peers.sort!{ |a, b| (a[:priority] <=> b[:priority]) || (b[:weight] <=> a[:weight])}
      Diameter.logger.debug("Sorted list of peers for realm #{realm} is #{possible_peers.inspect}")

      primary = possible_peers[0]

      url = "aaa://#{primary[:name]}:#{primary[:port]}"
      Diameter.logger.info("Primary peer for realm #{realm} is #{primary[:name]}, (#{url})")
      connect_to_peer(url, primary[:name], realm)
    end
@!group Peer connections and message sending
 Looks up the given Diameter realm with DNS-SRV, and establishes
 a connection to one peer in that realm.

 @param realm [String] The Diameter realm to connect to.
 @return [Peer] The Diameter peer chosen.