Heteroclinic orbits at infinity for two classes of Hamiltonian systems