bulk of the porting work. Tested on SmartOS 32-bit. 64-bit builds currently fail, they should hopefully be fixed soon.