gender ultrasound

Is gender ultrasound done internally or externally? And how long does it usually take? What's the earliest they can find gender?