//+------------------------------------------------------------------+
//|                                                  ReconnectEA.mq4 |
//|                                   Copyright © 2009, challenger78 |
//|                                                                  |
//| Original script by  talex.kaliningrad@gmail.com. 
//| I just made it into EA. 
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, challenger78"
#property link      ""
#property show_inputs
#import "user32.dll"
   int GetAncestor (int hWnd, int gaFlags);
   int GetDlgItem (int hDlg, int nIDDlgItem);
   int GetLastActivePopup (int hWndOwner);
   int PostMessageA (int hWnd, int Msg, int wParam, int lParam);
#import
#define WM_COMMAND         0x0111
#define WM_KEYDOWN         0x0100
#define VK_HOME            0x24
#define VK_ENTER           0x0D

extern int intervalinseconds=60;
int Pause=15000;  

void start() 
{
   while(true)
   {
      bool flag=true;

      while(!IsStopped())
  	   {
         if(!flag)
		   {
            Print("Connection restored. DC Time:",TimeToStr(TimeCurrent()));
            flag=true;
         }
         while(!IsConnected()&&!IsStopped()) {         
            if(flag) {
               Print("No connection to the server. DC Time:",TimeToStr(TimeCurrent()));
               flag=false;
            }
            Rescan();                                  
            Sleep(Pause);                              // wait to connect
            if(!IsConnected())
			   {                       // if no connection
               Reconnect();                            
               Sleep(Pause);                           // wait to connect
            }
         }
         Sleep(intervalinseconds*1000);
      }
      
      Sleep(1000);
   }
   return;
}
//+------------------------------------------------------------------+
void Rescan()
{
   int hwindow=GetAncestor(WindowHandle(Symbol(),Period()),2);// get the handle of the window
//-----------------------------------
	if(hwindow!=0)
	{                                 // find the main window
	   PostMessageA(hwindow,WM_COMMAND,37400,0);     // send message
	}
	else Print("Rescan Error:",GetLastError());
	return;
}
//-----------------------------------------------------------------------+
void Reconnect()
{
   int hwindow=WindowHandle(Symbol(),Period()),     // get window handle graphics
       hterminal=GetAncestor(hwindow,2);            // get the handle of the window
//---------------------------- 
   if(hterminal!=0)
	{                               // find the main window
      hwindow=GetDlgItem(hterminal,0xE81C);
      hwindow=GetDlgItem(hwindow,0x52);
      hwindow=GetDlgItem(hwindow,0x8A70);           // Favourites in Navigator window
      PostMessageA(hwindow,WM_KEYDOWN,VK_HOME,0);   // 
      PostMessageA(hwindow,WM_KEYDOWN,VK_ENTER,0);  // click login
      Sleep(1000);                                  // wait
      hwindow=GetLastActivePopup(hterminal);        // taking the form of a login
      PostMessageA(hwindow,WM_KEYDOWN,VK_ENTER,0);  // login
   }
   else Print("Reconnect Error:",GetLastError());
   return;
}
//-----------------------------------------------------------------------+