fixed client implementation - made as close to node.js as possible