Google Voice, Gizmo5 account (if you already have one; they're not accepting new requests) or a SkypeIn US number. Get a GSM smartphone with a simcard from a Canadian provider (since you're there most of the time). When in the states, get a pre-paid sim card with voice/data or just data. If you need the SkypeIn, it will set you back $30 for the year.
You now set up GV to forward calls to your US number to your VoIP account (Gizmo/Skype) while in Canada. Calls will be delivered via data. When in the states, you can continue with the same method, but with a prepaid simcard OR you can just forward via voice.
Note that while data plans for Canada or pre-paid US may be capped/metered you only need to use the GSM data when you are out-and-about. Any decent smartphone these days will happily shuffle data through wifi instead.
My Nokia N900 might be a bit too pricey, but will do everything here seamlessly with the built-in Skype and SIP integration.
The only thing this doesn't cover is porting your existing number to GoogleVoice...
I've noticed several design suggestions in your code.