/**
  * Initialize the XMPP connection.
  *
  * @param jid the jid to use
  * @param server the server to use (not using dns srv) may be null
  * @param port the port
  * @return the XMPPConnection prepared to connect
  */
 private Connection prepareConnection(String jid, String server, int port) {
   boolean useProxy = settings.getBoolean(BeemApplication.PROXY_USE_KEY, false);
   ProxyInfo proxyinfo = ProxyInfo.forNoProxy();
   if (useProxy) {
     String stype = settings.getString(BeemApplication.PROXY_TYPE_KEY, "HTTP");
     String phost = settings.getString(BeemApplication.PROXY_SERVER_KEY, "");
     String puser = settings.getString(BeemApplication.PROXY_USERNAME_KEY, "");
     String ppass = settings.getString(BeemApplication.PROXY_PASSWORD_KEY, "");
     int pport = Integer.parseInt(settings.getString(BeemApplication.PROXY_PORT_KEY, "1080"));
     ProxyInfo.ProxyType type = ProxyType.valueOf(stype);
     proxyinfo = new ProxyInfo(type, phost, pport, puser, ppass);
   }
   String serviceName = StringUtils.parseServer(jid);
   if (port != -1 || !TextUtils.isEmpty(server)) {
     if (port == -1) port = 5222;
     if (TextUtils.isEmpty(server)) server = serviceName;
     config = new ConnectionConfiguration(server, port, serviceName, proxyinfo);
   } else {
     config = new ConnectionConfiguration(serviceName, proxyinfo);
   }
   if (settings.getBoolean(BeemApplication.SMACK_DEBUG_KEY, false))
     config.setDebuggerEnabled(true);
   return new XMPPConnection(config);
 }