Fix close and flowcontrol

This may not work on windows. I have tested it rather extensively on OS X (10.8.x) and Angstrom Linux (BeagleBone Black) and seems quite stable.

-Rob