Move creating connection back out of loop and use connection sleep instead of time in order to keep the heartbeat alive