Added code for closing stream and limiting max clients