Major refactoring and rewrite

Refactor network handler into different protocols and implement generic
event loop. Refactor commonly used functions into common library. Move
configuration settings to config.lua. Extend the demo page.