Moving connection logic so it's in the loop.