Index: connection.cpp
===================================================================
--- connection.cpp	(revision 7577)
+++ connection.cpp	(working copy)
@@ -169,16 +169,18 @@
         newConn->prev = lastConn;
         lastConn->next = newConn;
     }
-    else if (connStr.IsEmpty())
-    {
-        LogMessage(wxString::Format(_("Failed to create new connection to database %s"), db.c_str()), LOG_WARNING);
-        LogMessage(wxString::Format(_("\tError message from connection : (%s)"), newConn->GetLastError().c_str()), LOG_WARNING);
-        return NULL;
-    }
     else
     {
-        LogMessage(wxString::Format(_("Failed to create new connection for connection string: %s"), connStr.c_str()), LOG_WARNING);
-        LogMessage(wxString::Format(_("\tError message from connection : (%s)"), newConn->GetLastError().c_str()), LOG_WARNING);
+        wxString warnMsg;
+        if (connStr.IsEmpty())
+            warnMsg
+              = wxString::Format(_("Failed to create new connection to database '%s':'%s'"),
+                                        db.c_str(), newConn->GetLastError().c_str());
+        else
+            warnMsg
+              = wxString::Format(_("Failed to create new connection for connection string '%s':%s"),
+                                        connStr.c_str(), newConn->GetLastError().c_str());
+        LogMessage(warnMsg, LOG_WARN_EXT);
         return NULL;
     }
 
Index: include/pgAgent.h
===================================================================
--- include/pgAgent.h	(revision 7577)
+++ include/pgAgent.h	(working copy)
@@ -39,7 +39,12 @@
 {
 	LOG_ERROR = 0,
 	LOG_WARNING,
-	LOG_DEBUG
+	LOG_DEBUG,
+	// NOTE:
+	//     "EXTENDED WARNING" will be used to log message for any LogLevel
+	//     Use it, only when initializing the pgAgent and to log error in
+  	//     connection
+	LOG_WARN_EXT = 15
 };
 
 // Prototypes
Index: win32.cpp
===================================================================
--- win32.cpp	(revision 7577)
+++ win32.cpp	(working copy)
@@ -31,10 +31,18 @@
 
 
 static bool serviceIsRunning;
+static bool pgagentInitialized;
 static HANDLE serviceSync;
 static HANDLE eventHandle;
 
+bool stopService();
 
+// This will be called from MainLoop, if pgagent is initialized properly
+void Initialized()
+{
+    pgagentInitialized = true;
+}
+
 // This will be called periodically to check if the service is to be paused.
 void CheckForInterrupt()
 {
@@ -70,8 +78,19 @@
                 break;
             case LOG_ERROR:
                 ReportEvent(eventHandle, EVENTLOG_ERROR_TYPE, 0, 0, NULL, 1, 0, tmp, NULL);
-                exit(1);
+                stopService();
+                // Make pgagent initialized to true, as initService waiting for it
+                // to be intialized
+                pgagentInitialized = true;
+                // Change service status
+                serviceStatus.dwCheckPoint=0;
+                serviceStatus.dwCurrentState=SERVICE_STOPPED;
+                SetServiceStatus(serviceStatusHandle, &serviceStatus);
                 break;
+            // Log the message as warning(for any LogLevel)
+            case LOG_WARN_EXT:
+                ReportEvent(eventHandle, EVENTLOG_WARNING_TYPE, 0, 0, NULL, 1, 0, tmp, NULL);
+                break;
         }
     }
     else
@@ -88,8 +107,13 @@
                 break;
             case LOG_ERROR:
                 wxPrintf(_("ERROR: %s\n"), msg);
+                pgagentInitialized = true;
                 exit(1);
                 break;
+            // Log the message as warning
+            case LOG_WARN_EXT:
+                wxPrintf(_("WARNING: %s\n"), msg);
+                break;
         }
     }
 }
@@ -99,7 +123,6 @@
 unsigned int __stdcall threadProcedure(void *unused)
 {
     MainLoop();
-
     return 0;
 }
 
@@ -188,6 +211,15 @@
 {
     ReleaseSemaphore(serviceSync, 1, 0);
     ResumeThread(threadHandle);
+#if START_SUSPENDED
+	while (!pgagentInitialized)
+	{
+		serviceStatusHandle.dwWaitHint += shortWait * 1000;
+		serviceStatusHandle.dwCheckPoint++;
+		SetServiceStatus(serviceStatusHandle, (LPSERVICE_STATUS) & serviceStatusHandle);
+		WaitAWhile();
+	}
+#endif
     return true;
 }
 
@@ -195,6 +227,7 @@
 {
     pauseService();
     CloseHandle (threadHandle);
+    threadHandle = 0;
     return true;
 }
 
@@ -203,10 +236,21 @@
     serviceSync = CreateSemaphore(0, 1, 1, 0);
 
     unsigned int tid;
+    pgagentInitialized = false;
 #if START_SUSPENDED
     threadHandle = (HANDLE)_beginthreadex(0, 0, threadProcedure, 0, CREATE_SUSPENDED, &tid);
 #else
     threadHandle = (HANDLE)_beginthreadex(0, 0, threadProcedure, 0, 0, &tid);
+    while (!pgagentInitialized)
+    {
+        if (eventHandle)
+        {
+            serviceStatus.dwWaitHint += shortWait * 1000 ;
+            serviceStatus.dwCheckPoint++;
+            SetServiceStatus(serviceStatusHandle, (LPSERVICE_STATUS) &serviceStatusHandle);
+        }
+        WaitAWhile();
+    }
 #endif
     return (threadHandle != 0);
 }
Index: unix.cpp
===================================================================
--- unix.cpp	(revision 7577)
+++ unix.cpp	(working copy)
@@ -64,6 +64,9 @@
         file.Write(_("ERROR: ") + msg + wxT("\n"));
         exit(1);
         break;
+    case LOG_WARN_EXT:
+        file.Write(_("WARNING: ") + msg + wxT("\n"));
+        break;
     }
 
     if (logFile.IsEmpty())
Index: pgAgent.cpp
===================================================================
--- pgAgent.cpp	(revision 7577)
+++ pgAgent.cpp	(working copy)
@@ -19,12 +19,17 @@
 wxString serviceDBname;
 wxString backendPid;
 long longWait=30;
-long shortWait=10;
+long shortWait=5;
 long minLogLevel=LOG_ERROR;
 
+#define MAXATTEMPT 10
+
 #ifndef __WXMSW__
 bool runInForeground = false;
 wxString logFile = wxEmptyString;
+#else
+//pgAgent Initialized
+void Initialized();
 #endif
 
 int MainRestartLoop(DBconn *serviceConn)
@@ -125,6 +130,7 @@
 
 void MainLoop()
 {
+    int attemptCount = 1;
     // OK, let's get down to business
     do
     {
@@ -191,12 +197,21 @@
                 LogMessage(strSchemaVerMisMatch, LOG_ERROR);
             }
 
+#ifdef WIN32
+			Initialized();
+#endif
             MainRestartLoop(serviceConn);
         }
 
-        LogMessage(wxString::Format(_("Couldn't create connection: %s"), serviceConn->GetLastError().c_str()), LOG_WARNING);
+        LogMessage(wxString::Format(_("ATTEMPT:%d:Couldn't create the primary connection:REASON:%s"), attemptCount, serviceConn->GetLastError().c_str()), LOG_WARN_EXT);
         DBconn::ClearConnections(true);
-        WaitAWhile(true);
+
+        // Try establishing primary connection upto MAXATTEMPT times
+        if (attemptCount++ >= (int)MAXATTEMPT)
+        {
+            LogMessage(wxString::Format(_("Stopping pgAgent: Couldn't establish the primary connection with the postgresql database server.")), LOG_ERROR);
+        }
+        WaitAWhile();
     }
     while (1);
 }
