Improve ephemeral port range selection. Fixes issue 3868