var EventEmitter = require('eventemitter2').EventEmitter2,
    util = require('util'),
    Discovery = require('./Discovery'),
    axon = require('axon'),
    Subscriber = require('./Subscriber'),
    Requester = require('./Requester');


var Sockend = function(io, advertisement, discoveryOptions) {
    EventEmitter.call(this, {
        wildcard: true, // should the event emitter use wildcards.
        delimiter: '::', // the delimiter used to segment namespaces, defaults to `.`.
        newListener: false, // if you want to emit the newListener event set to true.
        maxListeners: 2000 // the max number of listeners that can be assigned to an event, defaults to 10.
    });

    advertisement = advertisement || {};
    advertisement.axon_type = 'sockend';
    this.advertisement = advertisement;
    this.requesterTransformators = [];

    var that = this,
        d = Discovery(advertisement, discoveryOptions);

    var namespaces = {};

    d.on('added', function(obj) {
        if (obj.advertisement.axon_type != 'rep') return;
        if (obj.advertisement.key != advertisement.key) return;

        var namespace = obj.advertisement.namespace,
            normalizedNamespace =  namespace || '';

        if (namespaces['/' + normalizedNamespace]) return;

        namespaces['/' + normalizedNamespace] = true;
        obj.namespace = namespace;

        var requester = new Requester({
            name: 'sockendReq',
            namespace: namespace,
            key: that.advertisement.key
        }, discoveryOptions);
        obj.requester = requester;

        obj.requesterSocketHandler = function(socket) {
            obj.advertisement.respondsTo.forEach(function(topic) {
                socket.on(topic, function(data, cb) {
                    if (arguments.length == 1 && typeof data == 'function') {
                        cb = data;
                        data = {};
                    }

                    data.type = topic;

                    that.requesterTransformators.forEach(function(transFn) {
                        transFn(data, socket);
                    });

                    requester.send(data, cb);
                });
            });
        };

        requester.on('ready', function() {
            var server = io;
            if (namespace) server = io.of('/' + namespace);
            server.on('connection', obj.requesterSocketHandler);

            for (var sId in server.sockets) {
                obj.requesterSocketHandler(server.sockets[sId]);
            }
        });
    });

    var publisherNamespaces = {};

    d.on('added', function(obj) {
        if (obj.advertisement.axon_type != 'pub-emitter') return;
        if (obj.advertisement.key != advertisement.key) return;

        var namespace = obj.advertisement.namespace,
            normalizedNamespace =  namespace || '';

        if (publisherNamespaces['/' + normalizedNamespace]) return;

        publisherNamespaces['/' + normalizedNamespace] = true;
        obj.namespace = namespace;

        obj.advertisement.broadcasts && obj.advertisement.broadcasts.forEach(function(topic) {
            var parts = topic.split('::');
            if (parts.length > 1)  // if a topic includes a namespace
                io.of(parts[0]);   // allow sockets to connect to that namespace
        });

        var subscriber = new Subscriber({
            name: 'sockendSub',
            namespace: namespace,
            key: that.advertisement.key,
            subscribesTo: obj.advertisement.broadcasts
        }, discoveryOptions);

        obj.subscriber = subscriber;

        subscriber.on('**', function(data) {
            if (this.event == 'added' || this.event == 'removed') return;

            var topic = this.event.split('::'),
                namespace = '';

            if (topic.length > 1) {
                namespace += '/' + topic[0];
                topic = topic.slice(1)
            }

            topic = topic.join('');

            io.of(namespace).emit(topic, data);
        });
    });
};
util.inherits(Sockend, EventEmitter);

module.exports = Sockend;

@@ -2443,16 +2443,24 @@
 .sockets
+.sockets
 ) %7B%0A    
@@ -2512,16 +2512,24 @@
 .sockets
+.sockets
 %5BsId%5D);%0A

Fix access to socket.io sockets list (this probably changed in socket.io ~1.3.0)