Refactor code related to clients, channels and command lines

* remove duplicate code
* use constants instead of hard-coded values
* make code less reliant on a certain number of clients
* use arrays instead of individual 'numbered' variables
* reduce the number of arguments in function calls
* make conversion macros simpler and more robust
* number the clients starting from 0 instead of 1
* add assertions