beautify the code and check return code of connect