socket.io - 'connect' event does not fire on the client

I am trying to get started with node.js and socket.io.
I have a very (very) simple client-server test application and i can't get it to work.
The system is set up on a windows machine with an apache server.
The apache is running on port 81 and the the socket.io is set up to listen to port 8001

Server - server.js

var io = require('socket.io').listen(8001);

io.sockets.on('connection', function (socket) {
    console.log('\ngot a new connection from: ' + socket.id + '\n');
});

Client - index.html

<!DOCTYPE html>
<html>
<head>
    <title>node-tests</title>
    <script type="text/javascript" src="http://localhost:8001/socket.io/socket.io.js">    </script>
    <script type="text/javascript">
        var socket = io.connect('http://localhost', { port: 8001 });

        socket.on('connect', function () {
            alert('connect');
        });

        socket.on('error', function (data) {
            console.log(data || 'error');
        });

        socket.on('connect_failed', function (data) {
            console.log(data || 'connect_failed');
        });
    </script>
</head>
<body>

</body>
</html>

After starting my server using node server.js the standard (expected) output is written to the server console:

info: socket.io started

When I open index.html in the browser (chrome in my case - didn't test on other browsers) the following log is written to the server console:

info: socket.io started
debug: served static content /socket.io.js
debug: client authorized
info: handshake authorized 9952353481638352052
debug: setting request GET /socket.io/1/websocket/9952353481638352052
debug: set heartbeat interval for client 9952353481638352052
debug: client authorized for

got a new connection from: 9952353481638352052

debug: setting request GET /socket.io/1/xhr-polling/9952353481638352052?t=1333635235315
debug: setting poll timeout
debug: discarding transport
debug: cleared heartbeat interval for client 9952353481638352052
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635238316&i=0
debug: setting poll timeout
debug: discarding transport
debug: clearing poll timeout
debug: clearing poll timeout
debug: jsonppolling writing io.j[0]("8::");
debug: set close timeout for client 9952353481638352052
debug: jsonppolling closed due to exceeded duration
debug: setting request GET /socket.io/1/jsonp-polling/9952353481638352052?t=1333635258339&i=0
...
...
...

In the browsers console I get:

connect_failed

So it seems like the client is reaching the server and trying to connect, and the server is authorizing the handshake but the connect event is never fired in the client, instead the connect method reaches its connect timeout and max reconnection attempts and gives up returning connect_failed.

Any help\insight on this would be helpful.

Thank you

//Client side
<script src="http://yourdomain.com:8001/socket.io/socket.io.js"></script>
var socket = io.connect('http://yourdomain.com:8001');

//Server side
var io = require('socket.io').listen(8001);
io.sockets.on('connection',function(socket) {
`console.log('a user connected');`
});

Client side: The client side code requests the client version of socket IO from the same file that gives you server side socketIO. Var socket will now have all the methods that are available with socketIO, such as socket.emit or socket.on

Server Side: same as client side, making the socketIO methods available for use under var io.

Try the following:

Change

var socket = io.connect('http://localhost', { port: 8001 });

to

var socket = io.connect('http://localhost:8001');

Change in the frontend (client):

var socket = io.connect(http://myapp.herokuapp.com);

to

var socket = io.connect();

After many hours of dealing with various clients (Chrome, Firefox browsers and Phonegap app) that did not register connection to the server properly (i.e. the server registered the websocket connection successfully in its logs, but the frontend did not register the connection event), I tried @Timothy's solution from the comments and now everything works again on heroku and on localhost.

// server
var server = http.createServer(app).listen(8001);

// client
var socket = io.connect();

Thats because you are emmitting 'connect' event and server is listening for 'connection' event. Simply change the server side 'connection' event name to 'connect' event.

io.sockets.on('connect',function(socket){

Try using this:

Server.js

var http = require("http").createServer(), io = require("socket.io").listen(http);

http.listen(8001);

io.sockets.on("connection", function(socket){
    console.log("Connected!");
    socket.emit("foo", "Now, we are connected");
});

index.html

<script src="http://localhost:8001/socket.io/socket.io.js"></script>
<script>
     var i = io.connect("http://localhost:5001");
     i.on("foo", function(bar){
         console.log(bar);
     })
</script>

@jDubs

//Client
<script src="socket.io.js"></script>
var socket = io.connect('http://domain.com:8001');

//Server
var io = require('socket.io').listen(8001);
io.sockets.on('connection',function(socket) {
`console.log('user connected');`
});

I want to stress that using sockets via JavaScript is highly insecure. You should expect malicious hackers connecting to your server. You should be doing this on the PHP side. what js does is use an AJAX call to PHP. All of your js is view able by the client.

The only time i would ever suggest that you use sockets in PHP is if your going for cross platform real time such as a video game or something similar. There are many cases where you should just use a database. just make sure you are using a server for what it should actually be used for or you will be costing yourself precious performance.