Added code to prevent the main thread from executing while a connection is running