Reapplying realex gateway addition changes

@davidjrice fyi -- I reapplied your changes against most recent master. 
Ugh, I hate tests that modify global state. Can you mock this instead?