Fixing range memory errors

Also added python 3 support in socket code.