Swap websocket connections between multiple clients

I'm trying to monitor two computers, and show their information to a website. In the server I use javascript websocket and the two computers sends information to server IP. I want to monitor in the same page the two computers status and over 10 messages recived, swap the information to show.

This is my actual code, but that way connects the tow sockets at the same time. I want to open a socket with 1st IP, recive 10 messages, close connection and open the second IP. After reciveing 10 messages of IP2, close connecction, open socket with 1st IP and repeat the process.

It's better to use node.js ? I have never used it and i din't know the potential of this.

Can somebody help me ?

Sorry for my poor english.

var IPs =  ['ws://localhost:9000','ws://localhost:8000']; 

while(1){
    IPs.forEach(function(IPactual){
            var socket = new WebSocket(IPactual);
            console.log(socket.readyState);
            socket.onopen = function() {
                console.log('%c Connected to ' + socket.url, "color:orange ");
            }

            socket.onclose = function() {
                console.log('%c Disconnected from ' + socket.url, "color: red");
            }

            socket.onerror = function(e) {
                console.log('%c Ooops... ' + e, "color: red");
            }

            var messages= 0;
            socket.onmessage = function(e) {
                messages++;
                console.log(messages);

                // WRITE TO HTML THE INFORAMTION RECIVED

                if (messages==10){
                    socket.onclose = function () {}; 
                    socket.close()

                }
            }

    })
}

Thanks for the help, keeping opened the sockets I controlled it like this:

$(document).ready(function () {

    var n_messages_to_show = 10;
        var messages_control = 0;

        var socket = new WebSocket('ws://localhost:9000');

        socket.onopen = function() {
            console.log('%c Connected to ' + socket.url, "color:orange ");
        }

        socket.onclose = function() {
            console.log('%c Disconnected from ' + socket.url, "color: red");
        }

        socket.onerror = function(e) {
            console.log('%c Ooops... ' + e, "color: red");
        }

        var connections = 0;
        socket.onmessage = function(e) {
            if (messages_control<n_messages_to_show){
                console.log('%c Sending TAD1 monitor information ','background:lightgreen ; color: black');
                messages_control++;
                post_to_web(e);
            }
        }


        var socket2 = new WebSocket('ws://localhost:8000');

        socket2.onopen = function() {
            console.log('%c Connected to ' + socket2.url, "color:orange ");
        }

        socket2.onclose = function() {
            console.log('%c Disconnected from ' + socket2.url, "color: red");
        }

        socket2.onerror = function(e) {
            console.log('%c Ooops... ' + e, "color: red");
        }

        var connections = 0;
        socket2.onmessage = function(e) {
            if (messages_control>n_messages_to_show){
                console.log('%c Sending TAD2 monitor information ','background:lightgreen ; color: black');
                messages_control++;
                post_to_web(e);
            }
            if(messages_control==n_messages_to_show*2){
                messages_control=0;
            }
        }


    post_to_web = function(e){
         //############################################
         //              POST TO WEB
         //############################################
    }
});