Node.js throws an error that says: Can't set headers after they are sent

I'm learning node.js. The code below is taken from a book which is supposed to be working. It throws http.js:704 throw new Error('Can\'t set headers after they are sent.') if I access localhost:8080/stooges/chat

var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var catchPhrases = ['Why I oughta...', 'Nyuk Nyuk Nyuk!', 'Poifect!', 'Spread out!',   
'Say a few syllables!', 'Soitenly!'];

app.set('view engine', 'jade');
app.set('view options', { layout: true });
app.set('views', __dirname + '/views');

app.get('/stooges/chat', function(req, res, next) {
res.render('chat');
});

io.sockets.on('connection', function(socket) {
var sendChat = function( title, text ) {
    socket.emit('chat', {
        title: title,
        contents: text
    });
};

setInterval(function() {
    var randomIndex = Math.floor(Math.random()*catchPhrases.length)
    sendChat('Stooge', catchPhrases[randomIndex]);
}, 5000);

sendChat('Welcome to Stooge Chat', 'The Stooges are on the line');
socket.on('chat', function(data){
    sendChat('You', data.text);
});
});

app.get('/?', function(req, res){
res.render('index');
});

var port = 8080;
server.listen(port);
console.log('Listening on port ' + port);

Which line in the code above that triggers the error? And why?

UPDATE: I commented out the line var io = require('socket.io').listen(server); and it seems to be the one that's causing the error.

UPDATE: As requested by robertklep, here's the chat template:

extends layout

block scripts
script(type='text/javascript', src='/socket.io/socket.io.js')
script(type='text/javascript')
    var socket = io.connect('http://192.168.1.34:8080');
    socket.on('chat', function(data) {
        document.getElementById('chat').innerHTML =
        '<p><b>' + data.title + '</b>: ' + data.contents + '</p>';
    });
    var submitChat = function(form) {
        socket.emit('chat', {text: form.chat.value});
        return false;
    };

block content
div#chat

form(onsubmit='return submitChat(this);')
    input#chat(name='chat', type='text')
    input(type='submit', value='Send Chat')

So it turns out I was using the wrong version of socket.io. I updated the dependency in pakcage.json from 0.9.10 to 0.9.14 and it worked. Thanks for the help!