Description: Try to fetch database info from localhost first.
If the backend is bound to localhost, UPnP won't be activated, so this is
an alternative way to discover the backend connectivity information.

Origin: Mario Limonciello <superm1@ubuntu.com>
Bug: http://svn.mythtv.org/trac/ticket/8094
Bug-Ubuntu: http://bugs.debian.org/<bugnumber>
Last-Update: 2010-02-19

--- mythtv-0.23.0~trunk23567.orig/libs/libmyth/mythcontext.cpp
+++ mythtv-0.23.0~trunk23567/libs/libmyth/mythcontext.cpp
@@ -96,6 +96,7 @@ class MythContextPrivate : public QObjec
     bool    InitUPnP(void);
     void    DeleteUPnP(void);
     int     ChooseBackend(const QString &error);
+    int     TestLocal(void);
     int     UPnPautoconf(const int milliSeconds = 2000);
     void    StoreConnectionInfo(void);
     bool    DefaultUPnP(QString &error);
@@ -425,7 +426,10 @@ bool MythContextPrivate::FindDatabase(co
     // 3. Try to automatically find the single backend:
     if (autoSelect)
     {
-        int count = UPnPautoconf();
+        int count = TestLocal();
+
+        if (count == 0)
+            count = UPnPautoconf();
 
         if (count == 0)
             failure = "No UPnP backends found";
@@ -1079,6 +1083,38 @@ void MythContextPrivate::StoreConnection
     m_XML->Save();
 }
 
+/*
+ * Test localhost for a backend
+ * If bound to localhost, UPnP would be disabled anyway
+ * Only tries the standard status port
+ */
+
+int MythContextPrivate::TestLocal(void)
+{
+    QString host = "127.0.0.1";
+    int port = 6544;
+    bool connect = false;
+    if (telnet(host,port))
+    {
+        QString URL = "http://" + host + ":" + QString::number(port);
+        timeval ttExpires;
+        gettimeofday ( (&ttExpires), NULL );
+        DeviceLocation *device = new DeviceLocation(QString::null, QString::null, URL, ttExpires);
+        connect = UPnPconnect(device, QString::null);
+        if (!connect)
+        {
+            //Look if there is a pin in config.xml to try
+            XmlConfiguration *XML = new XmlConfiguration("config.xml");
+            QString PIN           = XML->GetValue(kDefaultPIN, "");
+            delete XML;
+            connect = UPnPconnect(device, PIN);
+        }
+        device->Release();
+        return connect;
+    }
+    return connect;
+}
+
 /**
  * If there is only a single UPnP backend, use it.
  *
@@ -1242,19 +1278,7 @@ bool MythContextPrivate::UPnPconnect(con
             VERBOSE(VB_UPNP, loc + error);
             break;
     }
-
-    // This backend may have a local DB with the default user/pass/DBname.
-    // For whatever reason, we have failed to get anything back via UPnP,
-    // so we might as well try the database directly as a last resort.
-    URL.remove("http://");
-    URL.remove(QRegExp("[:/].*"));
-    if (URL.isEmpty())
-        return false;
-
-    VERBOSE(VB_UPNP, "Trying default DB credentials at " + URL);
-    m_DBparams.dbHostName = URL;
-
-    return true;
+    return false;
 }
 
 /// If another thread has already started WOL process, wait on them...
