complete rewritten of tcp server, for stability