The most accurate time to determine the gender of a baby through an ultrasound or 4D scan is typically between 18 and 22 weeks of pregnancy. During this period, the genitalia are developed enough for identification. However, it's important to note that accuracy can vary depending on factors such as fetal position and technician experience. If you're specifically looking for gender confirmation, it's best to consult with your healthcare provider or ultrasound specialist for guidance on timing and availability of these scans.