more robust connection logic