/// <summary> /// Parse the given <b>IXmlElement</b> as a local <b>IPEndPoint</b>. /// </summary> /// <remarks> /// If the specified <b>IXmlElement</b> contains an empty address, /// <c>null</c> is returned. /// </remarks> /// <param name="xml"> /// The <b>IXmlElement</b> to parse. /// </param> /// <returns> /// A new <b>IPEndPoint</b> representing the contents of the given /// <b>XmlNode</b>. /// </returns> protected static IPEndPoint ParseLocalSocketAddress(IXmlElement xml) { IXmlElement xmlAddr = xml.GetElement("address"); IXmlElement xmlPort = xml.GetElement("port"); String sAddressFamiliy = xml.GetSafeElement("address-family").GetString( "InterNetwork"); if (xmlAddr == null && xmlPort == null) { return(null); } string addr = xmlAddr == null ? "localhost" : xmlAddr.GetString(); int port = xmlPort == null ? 0 : xmlPort.GetInt(); NetworkUtils.PreferredAddressFamily = (AddressFamily) Enum.Parse(typeof(AddressFamily), sAddressFamiliy); IPAddress ipAddress; try { ipAddress = addr.Equals("localhost") ? NetworkUtils.GetLocalHostAddress() : NetworkUtils.GetHostAddress(addr); } catch (Exception e) { throw new Exception("The \"" + xml.Name + "\" configuration " + "element contains an invalid \"address\" element", e); } try { return(new IPEndPoint(ipAddress, port)); } catch (Exception e) { throw new Exception("The \"" + xml.Name + "\" configuration " + "element contains an invalid \"port\" element", e); } }