var io          = {},
    fs          = require('fs'),
    _           = require('underscore'),
    request     = require('request'),
    newRelic    = require('newrelic'),
    snapshot    = require('node-snapshot'),
    config      = require('../config.json');

/**
 * @property url
 * @type {String}
 */
var url = config.api.host + '/products';

/**
 * @property products
 * @type {String}
 */
var products = '';

// Retrieve the products so we'll always have a fresh copy to serve to
// new connections.
request(url, function (error, response, body) {
    products = JSON.parse(body);
    io = require('socket.io').listen(parseInt(config.socket.port));
    beginListening();
});

/**
 * @method beginListening
 * @return {void}
 */
var beginListening = function beginListening() {

    /**
     * @on connection
     */
    io.sockets.on('connection', function (socket) {

        // Bootstrap Snapshot passing in the reference for the socket as a dependency.
        var $snapshot = new snapshot('products').bootstrap(socket).useDelta(true);

        // Configure the defaults.
        $snapshot.setPageNumber(1);
        $snapshot.setPerPage(15);
        $snapshot.setSortBy('name', 'asc');
        $snapshot.setRanges(['price']);
        $snapshot.setGroups(['manufacturer', 'colour']);
        $snapshot.setCollection(products, ['id', 'name', 'colour', 'price', 'manufacturer']);

        socket.on('snapshot/products/colours', function colours(ids) {

            $snapshot.applyFilter('colour', function colour(colourDimension) {

                colourDimension.filterFunction(function(d) {
                    return (d === 0) || _.contains(ids, d);
                });

            }, 'afresh');

        });

        socket.on('snapshot/products/manufacturers', function manufacturers(ids) {

            $snapshot.applyFilter('manufacturer', function manufacturer(manufacturerDimension) {

                manufacturerDimension.filterFunction(function(d) {
                    return (d === 0) || _.contains(ids, d);
                });

            }, 'afresh');

        });

        socket.on('disconnect', function() {
//        $snapshot = undefined;
        });

    });

};

@@ -512,48 +512,264 @@
 est(
-url, function (error, response, body) %7B%0A
+%7Burl:url,headers:%7B%22Accept%22: %22application/json%22%7D%7D, function (error, response, body) %7B%0A    if ((error) %7C%7C (response.statusCode!=200))%7B%0A        console.log(%22Error connecting to magento: %22+(error %7C%7C response.body));%0A        process.exit();%0A    %7D%0A    else%7B%0A    
     
@@ -797,16 +797,20 @@
 (body);%0A
+    
     io =
@@ -865,24 +865,28 @@
 ket.port));%0A
+    
     beginLis
@@ -895,16 +895,22 @@
 ning();%0A
+    %7D%0A
 %7D);%0A%0A/**

Fix connection to magento API in some cases in server.js