Refactor stuff from logic.c: connection stuff in connection.[ch],