added better https detection, connection error handling