Index: Dev/trunk/src/server/util/couch.js
===================================================================
--- Dev/trunk/src/server/util/couch.js	(revision 525)
+++ Dev/trunk/src/server/util/couch.js	(revision 527)
@@ -1,4 +1,5 @@
 var request = require('./request')
   , _ = require('underscore')
+  , HTTPResult = require('./http-result')
   ;
 
@@ -42,12 +43,13 @@
 }
 
-function couchRequest (method, url, body, opts) {
+function couchRequest (method, url, body, opts, retries) {
+    body = body || {};
+    retries = _.isNumber(retries) ? retries : 3;
     var options = {
         method: method,
-        headers: {
-            'Content-Type': 'application/json; charset=utf-8',
-            'Accept': 'application/json'
-        },
-        body: JSON.stringify(stringifyFunctions(body || {}))
+        headers: {},
+        body: stringifyFunctions(body),
+        json: true,
+        timeout: 5000
     };
     if (opts) {
@@ -56,8 +58,15 @@
     }
     return request(url, options)
-        .handle({
-            '-1': _.identity,
-            default: function(status,result) { return JSON.parse(result); }
-        });
+    .handle(function(status,body){
+        if ( _.isUndefined(body) ) {
+            if ( retries > 0) {
+                return couchRequest(method,url,body,opts,retries-1);
+            } else {
+                return new HTTPResult(-1,new Error("No response after several retries."));
+            }
+        } else {
+            return body;
+        }
+    });
 }
 
Index: Dev/trunk/src/server/util/http-result.js
===================================================================
--- Dev/trunk/src/server/util/http-result.js	(revision 525)
+++ Dev/trunk/src/server/util/http-result.js	(revision 527)
@@ -39,9 +39,14 @@
 // _fire :: Handler -> ()
 HTTPResult.prototype._fire = function(handler) {
-    var ret = handler.f(this.status,this.result);
-    if ( ret instanceof HTTPResult ) {
-        ret.handle(handler.next.set.bind(handler.next));
-    } else {
-        handler.next.set(this.status,ret);
+    try {
+        var ret = handler.f(this.status,this.result);
+        if ( ret instanceof HTTPResult ) {
+            ret.handle(handler.next.set.bind(handler.next));
+        } else {
+            handler.next.set(this.status,ret);
+        }
+    } catch(ex) {
+        console.log('HTTPResult','exception',ex.toString(),ex.stack);
+        handler.next.set(-1,ex);
     }
 };
Index: Dev/trunk/src/server/util/request.js
===================================================================
--- Dev/trunk/src/server/util/request.js	(revision 525)
+++ Dev/trunk/src/server/util/request.js	(revision 527)
@@ -1,5 +1,6 @@
 var HTTPResult = require('./http-result')
-  , request = require('request')
-  , url = require('url')
+  , HTTP = require('http')
+  , HTTPS = require('https')
+  , URL = require('url')
   , _ = require('underscore')
   ;
@@ -9,14 +10,63 @@
     var result = new HTTPResult();
     
-    options = options ? _.clone(options) : {};
-    options.uri = url || options.uri;
-    
-    request(options,function(err,res,body){
-        if ( err ) {
-            result.set(-1,err);
-        } else {
-            result.set(res.statusCode,body);
-        }
+    var body = options.body;
+    delete options.body;
+
+    var json = options.json;
+    delete options.json;
+
+    var reqOpts = _.isString(url) ? URL.parse(url) : url;
+    _.extend(reqOpts,options);
+    if ( json === true ) {
+        options.headers = options.headers || {};
+        _.extend(options.headers,{
+            'Content-Type': 'application/json',
+            'Accepts': 'application/json'
+        });
+    }
+
+    var protocol = reqOpts.protocol;
+    var httpModule;
+    if ( protocol === 'http:' ) {
+        httpModule = HTTP;
+    } else if ( protocol === 'https:' ) {
+        httpModule = HTTPS;
+    } else {
+        throw new Error("Unknown protocol "+protocol);
+    }
+
+    var req = httpModule.request(reqOpts);
+    req.on('response', function(response) {
+        var data = "";
+        response.setEncoding('utf8');
+        response.on('data', function(chunk) {
+            data += chunk;
+        });
+        response.on('end', function() {
+            var body;
+            try {
+                if ( json ) {
+                    if ( data ) { body = JSON.parse(data); }
+                } else {
+                    body = data;
+                }
+                result.set(response.statusCode,body);
+            } catch (ex) {
+                console.log('error',data);
+                result.set(-1,ex);
+            }
+        });
+        response.on('error', function(error) {
+            result.set(-1,error);
+        });
     });
+    req.on('error', function(error){
+        result.set(-1,error);
+    });
+
+    if ( body ) {
+        req.write(json ? JSON.stringify(body) : body);
+    }
+    req.end();
 
     return result;
