Refactor connection code to work at user level instead of session level