NPE prevention and fixing some socket nonsense