Redid the server code from Beej's guide