No valid themes are found on this instance

I am using Sugar CRM version 6.5.7 on PHP 5.3.28. I have recently migrated this application from Windows Server 2008 R2 to Windows 2016 Datacenter. 

The users on the application are getting this error randomly and there is no log for this particular exception. It's raising from ./include/SugarTheme/SugarTheme.php in the static function buildRegistry(). The themes are working and this issue also goes away with a page refresh so I am not sure what's missing. The themes folder has correct permission and is readable by the code. This issue happens even if the user has chosen the default theme. This issue was not there in Windows Server 2008 R2. Please help me here.

// default to setting the default theme as the current theme
if ( !isset($GLOBALS['sugar_config']['default_theme']) || !self::set($GLOBALS['sugar_config']['default_theme']) ) {
if ( count(self::availableThemes()) == 0 )
$counter = 0;
}while(count(self::availableThemes()) == 0 && $counter <= 5);

sugar_die('No valid themes are found on this instance');
} else {