Improved client management algorithm and fixed some minor bugs

Now the only thing which is left to fix is that server sometimes throws
stream exceptions and don't really know how to deal with them properly
but I'm working on it.