Try to consolidate some gateway connection code.