Port logic for popen (native version).