We study strong-weak coupling duality (S-duality) in N=4 supersymmetric nonAbelian Yang-Mills theories. These theories arise naturally as the low-energy limit of four-dimensional toroidal compactications of the heterotic string.Firstly, w e dene the free energy in the presence of electric and magnetic uxes using 't Hooft's prescription, i.e. through functional integrals at nite volume in the presence of twisted boundary conditions. Then, we compute those free energies in two limiting cases: small and large coupling constant.Finally, w e extend the free-energies to all values of the coupling constant (and the theta angle) by presenting a fully S-duality i n v ariant ansatz. This ansatz obeys all relevant consistency conditions; in particular, it obeys 't Hooft duality equations and Witten's magnetic-electric transmutation.The existence of an S-duality i n v ariant, consistent denition of free energies supports the claim made in the literature that S-duality is a duality symmetry of N=4 SUSY Yang-Mills. Our ansatz also suggests that N=4, irrespective of whether partially broken or not, is in a self-dual phase: no phase transitions occur between the strong and weak coupling regimes.